Skip to content

Commit dd2031d

Browse files
authored
Merge pull request #14 from RomainGoncalves/allow-for-http-auth
Allow for passing curl options
2 parents 30f7d71 + 49e7625 commit dd2031d

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,21 @@ This function accepts 2 parameters however, the second parameter is optional. T
7878
This will return to you the SimplePie object with the RSS feed in it.
7979
See [SimplePie](http://simplepie.org/api/index.html) API for all available methods.
8080

81+
#### Passing curl options
82+
You can also pass specific curl options per `read()` calls. You can pass these options, as an `array` as the 3rd parameter. The list of options can be found on the [PHP Manual](https://www.php.net/manual/en/function.curl-setopt.php).
83+
84+
Example:
85+
```php
86+
// You need to log in to the rss endpoint with a Digest auth
87+
$options = [
88+
'curl_options' => [
89+
CURLOPT_HTTPAUTH => CURLAUTH_DIGEST,
90+
CURLOPT_USERPWD => 'username:password'
91+
]];
92+
93+
$f = FeedReader::read('https://news.google.com/news/rss', 'default', $options);
94+
```
95+
8196
## License
8297

8398
Feed Reader is free software distributed under the terms of the MIT license

src/FeedReader.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@ public function __construct(Container $app)
2828
*
2929
* @param $url
3030
* @param string $configuration
31+
* @param array $options
3132
* @return SimplePie
3233
*/
33-
public function read($url, $configuration = 'default')
34+
public function read($url, $configuration = 'default', array $options = [])
3435
{
3536
// Setup the object
3637
$sp = $this->app->make(SimplePie::class);
@@ -62,6 +63,11 @@ public function read($url, $configuration = 'default')
6263
]);
6364
}
6465

66+
// If the user passes manual curl options, let's add them
67+
if (isset($options['curl_options'])) {
68+
$sp->set_curl_options($options['curl_options']);
69+
}
70+
6571
// Set the feed URL
6672
$sp->set_feed_url($url);
6773

tests/Unit/FeedReaderTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,28 @@ public function testReadRss()
3030
$this->assertEquals('FeedForAll Sample Feed', $rss->get_title());
3131
$this->assertObjectHasAttribute('term', $rss->get_category());
3232
$this->assertEquals(9, $rss->get_item_quantity());
33+
// No curl options passed
34+
$this->assertEquals([], $rss->curl_options);
35+
36+
self::$process->stop();
37+
}
38+
39+
public function testReadRssWithOptions()
40+
{
41+
self::$process = new Process(['php', '-S', 'localhost:8123', '-t', './tests/resources']);
42+
self::$process->start();
43+
usleep(500000);
44+
45+
/** @var SimplePie $rss */
46+
$rss = FeedReaderFacade::read('http://localhost:8123/rss.xml', null, ['curl_options' => [
47+
CURLOPT_HTTPAUTH => CURLAUTH_DIGEST,
48+
CURLOPT_USERPWD => 'username:password'
49+
]]);
50+
51+
$this->assertEquals([
52+
107 => 2,
53+
10005 => "username:password"
54+
], $rss->curl_options);
3355

3456
self::$process->stop();
3557
}

0 commit comments

Comments
 (0)