0

I need to view the content of an RSS through PHP, but the problem is that it needs an authentication with username and password, credentials that I have.

How can I print the content of it in my file?

this is what I get when I visit my feed page

enter image description here

EnexoOnoma
  • 8,454
  • 18
  • 94
  • 179

2 Answers2

2

cURL may be useful to you here to get the contents of the RSS feed. Pay special attention to the CURLOPT_USERPWD option:

$process = curl_init($host);
curl_setopt($process, CURLOPT_HTTPHEADER, array('Content-Type: application/xml', $additionalHeaders));
curl_setopt($process, CURLOPT_HEADER, 1);
curl_setopt($process, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($process, CURLOPT_TIMEOUT, 30);
curl_setopt($process, CURLOPT_POST, 1);
curl_setopt($process, CURLOPT_POSTFIELDS, $payloadName);
curl_setopt($process, CURLOPT_RETURNTRANSFER, TRUE);
$return = curl_exec($process);
curl_close($process);

For more information, check out this SO question.

Community
  • 1
  • 1
esqew
  • 42,425
  • 27
  • 92
  • 132
  • thank you for this, i am trying it now. What to I add to the `$payloadName` variable? – EnexoOnoma Apr 23 '14 at 18:54
  • @Karoumpas - for information on the `CURLOPT_POSTFIELDS` option, check out [this page](http://php.net/manual/en/function.curl-setopt.php) in the PHP manual. Basically, it's setting POST fields for your request, which may not be applicable in your situation. – esqew Apr 23 '14 at 19:15
1

To get the contents of a file with authentication headers (basic auth), you could do something like:

<?php

$url = 'http://www.thelinktorss.com/feed.xml';
$username = 'USER';
$password = 'PASS';

$context = stream_context_create(array('http' => array('header'  => "Authorization: Basic " . base64_encode($username . ':' . $password))));

$feed = file_get_contents($url, false, $context);

echo $feed;
Thomas Lomas
  • 1,553
  • 10
  • 22