简体   繁体   中英

Curl request fails

I have a small issue. I want to load this page 'http://www.facebook.com/feeds/page.php?id=57084011597&format=rss20' using curl. Here is the code I am trying t use, nut I can't get aything back from it.

    $head=array('Accept'=>'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
                    'Accept-Charset'=>'ISO-8859-2,utf-8;q=0.7,*;q=0.3',
                    'Accept-Encoding'=>'gzip,deflate,sdch',
                    'Accept-Language'=>'ro-RO,ro;q=0.8,en-US;q=0.6,en;q=0.4',
                    'Cache-Control'=>'max-age=0',
                    'Connection'=>'keep-alive',
                    'Cookie'=>'datr=hroHTi2NZk2KleOaswb03Q_Q; lu=gg9lJcPeInHt6hnut7bviqQg; locale=en_US; e=n; L=2; c_user=100000596376783; sct=1309129360; xs=2%3Ad05dd80e364608525dd664ad73f6483f; act=1309410851554%2F5; presence=EM309410852L4N0_5dEp_5f1B00596376783F1X309410852168Y0Z11G309410768PCC',
                    'Host'=>'www.facebook.com',
                    'User-Agent'=>$_SERVER['HTTP_USER_AGENT']);  
$url='http://www.facebook.com/feeds/page.php?id=57084011597&format=rss20';
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,true); 
    curl_setopt($ch, CURLOPT_TIMEOUT, 20); 
    curl_setopt($ch, CURLOPT_HTTPHEADER,$head);
     $result = curl_exec($ch);

        return $result;

I've tried without setting a header but it also didn't worked. Hope that somebody coud give me a clue.

Thank you!

This is your problem: $ur='http://www.facebook.com/feeds/page.php?id=57084011597&format=rss20'; . You're missing an "l".

$url='http://www.facebook.com/feeds/page.php?id=57084011597&format=rss20';

Also, there's a redirect so you'll need to add:

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

This how I found out. I enabled verbose output using curl_setopt($ch, CURLOPT_VERBOSE, true); and saw this:

< HTTP/1.1 302 Found
< Location: http://www.facebook.com/common/browser.php
< Content-Type: text/html; charset=utf-8
< X-FB-Server: 10.43.97.37
< X-Cnection: close
< Date: Thu, 30 Jun 2011 06:48:54 GMT
< Content-Length: 0

Update, tested and working:

Facebook expects a user-agent string to be specified. Setting it with curl_setopt($ch, CURLOPT_USERAGENT, '...');seems to solve the problem. Here's an over-simplistic example which should solve your problem:

<?php

$ch = curl_init('http://www.facebook.com/feeds/page.php?id=57084011597&format=rss20');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, array('User-Agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Safari/534.45'));

$response = curl_exec($ch);

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