Skip to content
This repository has been archived by the owner on Jun 12, 2024. It is now read-only.

Commit

Permalink
Merge pull request #11 from kasp3r/version-update
Browse files Browse the repository at this point in the history
* Updated guzzle and PHPUnit to the latest stable versions
  • Loading branch information
kasp3r committed Apr 14, 2016
2 parents 0314927 + eca7794 commit 468c020
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 38 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
language: php

php:
- 5.4
- 5.5
- 5.6
- 7

before_script:
- composer install --dev
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ A PHP library to easily get website information (title, description, image...) f

## Dependencies

* PHP >= 5.4
* PHP >= 5.6 (without phpunit it should work on 5.5)
* Guzzle

## Installation
Expand Down Expand Up @@ -48,6 +48,7 @@ foreach ($parsed as $parserName => $link) {
echo $link->getTitle() . PHP_EOL;
echo $link->getDescription() . PHP_EOL;
echo $link->getImage() . PHP_EOL;
print_r($link->getPictures());
}
```

Expand All @@ -62,6 +63,14 @@ https://github.com/
GitHub · Build software better, together.
GitHub is the best place to build software together. Over 10.1 million people use GitHub to share code.
https://assets-cdn.github.com/images/modules/open_graph/github-octocat.png
Array
(
[0] => https://assets-cdn.github.com/images/modules/site/home-ill-build.png?sn
[1] => https://assets-cdn.github.com/images/modules/site/home-ill-work.png?sn
[2] => https://assets-cdn.github.com/images/modules/site/home-ill-projects.png?sn
[3] => https://assets-cdn.github.com/images/modules/site/home-ill-platform.png?sn
[4] => https://assets-cdn.github.com/images/modules/site/org_example_nasa.png?sn
)
```

###Youtube example
Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
}
],
"require": {
"php": ">=5.3.0",
"guzzle/guzzle": "~3.8"
"php": ">=5.6",
"guzzlehttp/guzzle": "^6.2"
},
"require-dev": {
"phpunit/phpunit": "3.7.*"
"phpunit/phpunit": "^5.3"
},
"autoload": {
"psr-0": {
Expand Down
24 changes: 24 additions & 0 deletions src/LinkPreview/Model/Link.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ class Link implements LinkInterface
* @var string $image Url to image
*/
private $image;
/**
* @var array $pictures Urls to all images on a page
*/
private $pictures;
/**
* @var string $realUrl
*/
Expand Down Expand Up @@ -171,4 +175,24 @@ public function setUrl($url)

return $this;
}

/**
* @param array $pictures
* @return $this
*/
public function setPictures($pictures)
{
$this->pictures = $pictures;

return $this;
}

/**
* Get Urls to all images on a page
* @return array
*/
public function getPictures()
{
return $this->pictures;
}
}
13 changes: 13 additions & 0 deletions src/LinkPreview/Model/LinkInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ public function getDescription();
*/
public function getImage();

/**
* Get pictures urls
* @return array
*/
public function getPictures();

/**
* Get real url after all redirects
* @return string
Expand Down Expand Up @@ -77,6 +83,13 @@ public function setDescription($description);
*/
public function setImage($image);

/**
* Set pictures urls
* @param array $pictures
* @return $this
*/
public function setPictures($pictures);

/**
* Set real url after all redirects
* @param string $realUrl
Expand Down
12 changes: 9 additions & 3 deletions src/LinkPreview/Parser/GeneralParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class GeneralParser implements ParserInterface

/**
* @param ReaderInterface $reader
* @param LinkInterface $link
* @param LinkInterface $link
*/
public function __construct(ReaderInterface $reader = null, LinkInterface $link = null)
{
Expand Down Expand Up @@ -134,7 +134,8 @@ public function parseLink()

$link->setTitle($htmlData['title'])
->setDescription($htmlData['description'])
->setImage($htmlData['image']);
->setImage($htmlData['image'])
->setPictures($htmlData['pictures']);
} elseif (!strncmp($link->getContentType(), 'image/', strlen('image/'))) {
$link->setImage($link->getRealUrl());
}
Expand All @@ -152,7 +153,8 @@ protected function parseHtml($html)
$data = [
'image' => '',
'title' => '',
'description' => ''
'description' => '',
'pictures' => [],
];

libxml_use_internal_errors(true);
Expand Down Expand Up @@ -200,6 +202,10 @@ protected function parseHtml($html)
}
}

foreach ($doc->getElementsByTagName('img') as $img) {
$data['pictures'][] = $img->getAttribute('src');
}

return $data;
}

Expand Down
29 changes: 22 additions & 7 deletions src/LinkPreview/Reader/GeneralReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace LinkPreview\Reader;

use Guzzle\Http\Client;
use GuzzleHttp\Client;
use GuzzleHttp\RequestOptions;
use GuzzleHttp\TransferStats;
use LinkPreview\Model\LinkInterface;

/**
Expand All @@ -25,7 +27,7 @@ class GeneralReader implements ReaderInterface
public function getClient()
{
if (!$this->client) {
$this->client = new Client();
$this->client = new Client([RequestOptions::COOKIES => true]);
}

return $this->client;
Expand Down Expand Up @@ -65,12 +67,25 @@ public function readLink()
$link = $this->getLink();

$client = $this->getClient();
$client->setBaseUrl($link->getUrl());
$response = $client->get()->send();
$response = $client->request(
'GET',
$link->getUrl(),
[
'on_stats' => function (TransferStats $stats) use (&$effectiveUrl) {
$effectiveUrl = $stats->getEffectiveUri();
}
]
);

$link->setContent($response->getBody(true))
->setContentType($response->getContentType())
->setRealUrl($response->getEffectiveUrl());
$headerContentType = $response->getHeader('content-type');
$contentType = '';
if (is_array($headerContentType) && count($headerContentType) > 0) {
$contentType = current(explode(';', current($headerContentType)));
}

$link->setContent((string)$response->getBody())
->setContentType($contentType)
->setRealUrl($effectiveUrl);

return $link;
}
Expand Down
29 changes: 7 additions & 22 deletions tests/LinkPreview/Tests/Reader/GeneralReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ class GeneralReaderTest extends \PHPUnit_Framework_TestCase
public function testReadLink()
{
$responseMock = $this->getMock(
'Guzzle\Http\Message\Response',
['getBody', 'getContentType', 'getEffectiveUrl'],
'GuzzleHttp\Psr7\Response',
['getBody', 'getHeader'],
[],
'',
false
Expand All @@ -19,27 +19,13 @@ public function testReadLink()
->method('getBody')
->will(self::returnValue('body'));
$responseMock->expects(self::once())
->method('getContentType')
->will(self::returnValue('text/html'));
$responseMock->expects(self::once())
->method('getEffectiveUrl')
->will(self::returnValue('http://github.com'));

$requestMock = $this->getMock(
'Guzzle\Http\Message\Request',
['send'],
[],
'',
false
);
$requestMock->expects(self::once())
->method('send')
->will(self::returnValue($responseMock));
->method('getHeader')
->will(self::returnValue(array('text/html; UTF-8')));

$clientMock = $this->getMock('Guzzle\Http\Client');
$clientMock = $this->getMock('GuzzleHttp\Client');
$clientMock->expects(self::once())
->method('get')
->will(self::returnValue($requestMock));
->method('request')
->will(self::returnValue($responseMock));

$linkMock = $this->getMock('LinkPreview\Model\Link', null);

Expand All @@ -50,6 +36,5 @@ public function testReadLink()

self::assertEquals('body', $link->getContent());
self::assertEquals('text/html', $link->getContentType());
self::assertEquals('http://github.com', $link->getRealUrl());
}
}

0 comments on commit 468c020

Please sign in to comment.