簡體   English   中英

Curl 請求失敗

[英]Curl request fails

我有一個小問題。 我想加載這個頁面'http://www.facebook.com/feeds/page.php?id=57084011597&format=rss20'使用ZF6E57C9DE7515E43548ZF9。 這是我嘗試使用的代碼,但我無法從中獲得任何回報。

    $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;

我試過沒有設置 header 但它也沒有用。 希望有人能給我一個線索。

謝謝!

這是你的問題: $ur='http://www.facebook.com/feeds/page.php?id=57084011597&format=rss20'; . 你少了一個“l”。

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

此外,還有一個重定向,因此您需要添加:

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

我是這樣發現的。 我使用curl_setopt($ch, CURLOPT_VERBOSE, true); 看到了這個:

< 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

更新、測試和工作:

Facebook 需要指定用戶代理字符串。 使用curl_setopt($ch, CURLOPT_USERAGENT, '...');設置它似乎可以解決問題。 這是一個過於簡單的示例,應該可以解決您的問題:

<?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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM