I’m trying to retrieve XML data from my wordpress blog using CURL.
Here is my PHP:
$ch = curl_init("http://www.hazelandruby.com/blog/feed");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/xml') );
$data = curl_exec($ch);
print_r(curl_getinfo($ch));
curl_close($ch);//close connection
unset($ch);
var_dump($data);
$data
is an empty string.
curl_getInfo
gives me this information:
Array ( [url] => http://www.hazelandruby.com/blog/feed [content_type] => text/html [http_code] => 301 [header_size] => 335 [request_size] => 99 [filetime] => -1 [ssl_verify_result] => 0 [redirect_count] => 0 [total_time] => 0.039749 [namelookup_time] => 0.001784 [connect_time] => 0.001862 [pretransfer_time] => 0.001866 [size_upload] => 0 [size_download] => 0 [speed_download] => 0 [speed_upload] => 0 [download_content_length] => -1 [upload_content_length] => 0 [starttransfer_time] => 0.03974 [redirect_time] => 0 [certinfo] => Array ( ) [redirect_url] => http://www.hazelandruby.com/blog/feed/ )
You can access the XML file itself: http://www.hazelandruby.com/blog/feed/
Ultimately I want to be able to create a SimpleXmlElement with this but it’s not working. Do I have my PHP right? Is the wordpress outputting the correct XML? If not (I prefer not to change what wordpress is doing), how can I interpret the url as XML and get the data?
Based on the headers returned by your
curl_getinfo()
call, specifically[http_code] => 301
, the request is being redirected but you haven’t told curl it’s allowed to follow it.To do this, you’ll have to set the
CURLOPT_FOLLOWLOCATION
totrue
:Also, as it’s relevant, the URL it’s redirecting to is
http://www.hazelandruby.com/blog/feed/
, your original URL with an appended/
. You should be able to “fix” the issue by adding the ending/
to the original URL (hopefully =P).