Skip to content

Commit 781a941

Browse files
committed
Merge pull request #27 from z38/feature-referer
Add option to set referer
2 parents 28533fe + 384aa0b commit 781a941

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

lib/Madcoda/Youtube.php

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ class Youtube
1313
*/
1414
protected $youtube_key; //pass in by constructor
1515

16+
/**
17+
* @var string
18+
*/
19+
protected $referer;
20+
1621
/**
1722
* @var array
1823
*/
@@ -28,7 +33,7 @@ class Youtube
2833
/**
2934
* @var array
3035
*/
31-
var $page_info = array();
36+
public $page_info = array();
3237

3338
/**
3439
* Constructor
@@ -37,12 +42,19 @@ class Youtube
3742
* @param array $params
3843
* @throws \Exception
3944
*/
40-
public function __construct($params)
45+
public function __construct($params = array())
4146
{
42-
if (is_array($params) && array_key_exists('key', $params)) {
43-
$this->youtube_key = $params['key'];
44-
} else {
45-
throw new \Exception('Google API key is Required, please visit http://code.google.com/apis/console');
47+
if (!is_array($params)) {
48+
throw new \InvalidArgumentException('The configuration options must be an array.');
49+
}
50+
51+
if (!array_key_exists('key', $params)) {
52+
throw new \InvalidArgumentException('Google API key is required, please visit http://code.google.com/apis/console');
53+
}
54+
$this->youtube_key = $params['key'];
55+
56+
if (array_key_exists('referer', $params)) {
57+
$this->referer = $params['referer'];
4658
}
4759
}
4860

@@ -454,6 +466,9 @@ public function api_get($url, $params)
454466
} else {
455467
curl_setopt($tuCurl, CURLOPT_PORT, 443);
456468
}
469+
if ($this->referer !== null) {
470+
curl_setopt($tuCurl, CURLOPT_REFERER, $this->referer);
471+
}
457472
curl_setopt($tuCurl, CURLOPT_RETURNTRANSFER, 1);
458473
$tuData = curl_exec($tuCurl);
459474
if (curl_errno($tuCurl)) {

test/Madcoda/Tests/YoutubeTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class YoutubeTest extends \PHPUnit_Framework_TestCase
1818
/**
1919
* @var Youtube
2020
*/
21-
var $youtube;
21+
protected $youtube;
2222

2323
public function setUp()
2424
{
@@ -40,15 +40,15 @@ public function MalFormURLProvider()
4040
}
4141

4242
/**
43-
* @expectedException \Exception
43+
* @expectedException \InvalidArgumentException
4444
*/
4545
public function testConstructorFail()
4646
{
4747
$this->youtube = new Youtube(array());
4848
}
4949

5050
/**
51-
* @expectedException \Exception
51+
* @expectedException \InvalidArgumentException
5252
*/
5353
public function testConstructorFail2()
5454
{

0 commit comments

Comments
 (0)