简体   繁体   中英

PHP CURL curl_exec not returning anything

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 to true :

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM