0

我的網頁使用PHP來做到這一點得到了登錄用戶的朋友算一個FQL請求,下面的代碼:間歇HTTP400(壞請求)使用FQL與file_get_contents()函數時

$graph = 'https://graph.facebook.com/fql?q='; 
$graph .= 'SELECT+friend_count+FROM+user+WHERE+uid%3Dme%28%29'; 
$graph .= '&access_token=' . $access_token; 
$result = file_get_contents($graph); 

約80%這工作正常的時間,但有時我得到一個400壞請求。我注意到這似乎是由'&'和'access_token'分隔的&符號所引起的;即我得到這個(不工作):

https://graph.facebook.com/fql?q=SELECT+friend_count+FROM+user+WHERE+uid%3Dme%28%29&access_token=AAAF8VR3YZCpQBAGiX16jvZAwaEciTwZB1QZAaUjcjy82Ce7Ov7nPqNxjsKM1SAZASGVcZCJ80R9KJZBJYrjKmsDVK6YNrPGA7plPVuwCFZCaOwZDZD 

代替此的(作品 - 同樣的要求,但 '&放大器;' 由 '&' 已取代):

https://graph.facebook.com/fql?q=SELECT+friend_count+FROM+user+WHERE+uid%3Dme%28%29&access_token=AAAF8VR3YZCpQBAGiX16jvZAwaEciTwZB1QZAaUjcjy82Ce7Ov7nPqNxjsKM1SAZASGVcZCJ80R9KJZBJYrjKmsDVK6YNrPGA7plPVuwCFZCaOwZDZD 

我試着相應地調整我的代碼,使PHP明確地告訴字符串替換逃脫符號,但無濟於事:

$graph = 'https://graph.facebook.com/fql?q='; 
$graph .= 'SELECT+friend_count+FROM+user+WHERE+uid%3Dme%28%29'; 
$graph .= '&access_token=' . $access_token; 
$result = file_get_contents($graph); 
$graphNoEscape = str_replace('&', '&', $graph); 
$result = file_get_contents($graphNoEscape); 

有誰知道解決這個問題?令人討厭的是,這段代碼有時會起作用,但並非全是!

回答

2

對任何人說這個發現,並有同樣的問題,這是我如何解決它:

首先,我告訴服務器使用中的public_html/WWW文件夾一個php.ini文件,PHP版本5.3。

然後我修改了代碼如下:

$graph = 'https://graph.facebook.com/fql?q='; 
$graph .= urlencode('SELECT friend_count FROM user WHERE uid = me()'); 
$graph .= '&access_token='.$access_token; 
$graphNoEscape = str_replace('&', '&', $graph); 
$file_contents = file_get_contents($graphNoEscape); 
$fql_result = json_decode($file_contents); 
$friend_count = mysql_real_escape_string($fql_result->data[0]->friend_count); 

誠然,我可能已經在與str_replace()函數做了,但它的作品沒有最少:)

希望這可以幫助有人。

+0

'&'只在URL中使用'&'字符作爲HTML屬性的一部分時才需要(但不是絕對必需)。通常情況下,它不應該出現在URL中。 – hayavuk