Skip to content

Commit 6c1647b

Browse files
committed
fixed tests, added UserAgent class
1 parent 49d4604 commit 6c1647b

File tree

4 files changed

+56
-5
lines changed

4 files changed

+56
-5
lines changed

src/Http/CurlDispatcher.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class CurlDispatcher implements DispatcherInterface
2121
CURLOPT_ENCODING => '',
2222
CURLOPT_AUTOREFERER => true,
2323
CURLOPT_FOLLOWLOCATION => true,
24-
CURLOPT_USERAGENT => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.95 Safari/537.36',
24+
CURLOPT_USERAGENT => 'Embed PHP library',
2525
CURLOPT_IPRESOLVE => CURL_IPRESOLVE_V4,
2626
];
2727

@@ -81,6 +81,10 @@ public function dispatch(Url $url)
8181
curl_setopt_array($connection, $this->config);
8282
curl_setopt($connection, CURLOPT_HTTPHEADER, ['Accept: text/html']);
8383

84+
if (($useragent = UserAgent::resolve($url)) !== null) {
85+
curl_setopt($connection, CURLOPT_USERAGENT, $useragent);
86+
}
87+
8488
$curl = new CurlResult($connection);
8589

8690
//Get only text responses

src/Http/UserAgent.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace Embed\Http;
4+
5+
/**
6+
* Class to return the user agent used depending of the url.
7+
* This is because some sites works better with some user agents
8+
*/
9+
abstract class UserAgent
10+
{
11+
private static $patterns = [
12+
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.95 Safari/537.36' => [
13+
]
14+
];
15+
16+
public static function add($useragent, $pattern)
17+
{
18+
if (!isset(self::$patterns[$useragent])) {
19+
self::$patterns[$useragent] = (array) $pattern;
20+
return;
21+
}
22+
23+
$pattern = (array) $pattern;
24+
25+
foreach ($pattern as $v) {
26+
if (!in_array($v, self::$patterns[$useragent])) {
27+
self::$patterns[$useragent][] = $v;
28+
}
29+
}
30+
}
31+
32+
/**
33+
* Returns the user agent.
34+
*
35+
* @param Url $url
36+
*
37+
* @return string|null
38+
*/
39+
public static function resolve(Url $url)
40+
{
41+
foreach (self::$patterns as $useragent => $pattern) {
42+
if ($url->match($pattern)) {
43+
return $useragent;
44+
}
45+
}
46+
}
47+
}

tests/FileTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ public function testPdf()
3131
public function testOgg()
3232
{
3333
$this->assertEmbed(
34-
'https://upload.wikimedia.org/wikipedia/commons/b/b5/I-15bis.ogg',
34+
'https://ia801006.us.archive.org/34/items/popeye_349/popeye.ogv',
3535
[
3636
'type' => 'video',
37-
'code' => '<video controls><source src="https://upload.wikimedia.org/wikipedia/commons/b/b5/I-15bis.ogg"></video>',
38-
'providerUrl' => 'https://wikimedia.org',
37+
'code' => '<video controls><source src="https://ia801006.us.archive.org/34/items/popeye_349/popeye.ogv"></video>',
38+
'providerUrl' => 'https://archive.org',
3939
]
4040
);
4141
}

tests/WikipediaTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class WikipediaTest extends AbstractTestCase
77
public function testOne()
88
{
99
$description = <<<'EOT'
10-
Albert Einstein (/ˈaɪnstaɪn/; German: [ˈalbɛɐ̯t ˈaɪnʃtaɪn]; 14 March 1879 – 18 April 1955) was a German-born theoretical physicist. He developed the general theory of relativity, one of the two pillars of modern physics (alongside quantum mechanics). Einstein's work is also known for its influence on the philosophy of science. Einstein is best known in popular culture for his mass–energy equivalence formula E = mc2 (which has been dubbed "the world's most famous equation"). He received the 1921 Nobel Prize in Physics for his "services to theoretical physics", in particular his discovery of the law of the photoelectric effect, a pivotal step in the evolution of quantum theory. Near the beginning of his career, Einstein thought that Newtonian mechanics was no longer enough to reconcile the laws of classical mechanics with the laws of the electromagnetic field. This led him to develop his special theory of relativity. He realized, however, that the principle of relativity could also be extended to gravitational fields, and with...
10+
Albert Einstein (/ˈaɪnstaɪn/; German: [ˈalbɛɐ̯t ˈaɪnʃtaɪn]; 14 March 1879 – 18 April 1955) was a German-born theoretical physicist. He developed the general theory of relativity, one of the two pillars of modern physics (alongside quantum mechanics). Einstein's work is also known for its influence on the philosophy of science. Einstein is best known in popular culture for his mass–energy equivalence formula E = mc2 (which has been dubbed "the world's most famous equation"). He received the 1921 Nobel Prize in Physics "for his services to Theoretical Physics, and especially for his discovery of the law of the photoelectric effect", a pivotal step in the evolution of quantum theory. Near the beginning of his career, Einstein thought that Newtonian mechanics was no longer enough to reconcile the laws of classical mechanics with the laws of the electromagnetic field. This led him to develop his special theory of relativity. He realized, however, that the principle of relativity could also be extended to gravitational fields, and...
1111
EOT;
1212

1313
$this->assertEmbed(

0 commit comments

Comments
 (0)