2010-07-20 82 views
1

我有點卡住了。我開發了一個腳本,由於某種原因,它自動將URL中的&轉換爲&什麼會導致&URLS自動轉換爲&?

這是造成問題的行:

$accessToken = file_get_contents('https://graph.facebook.com/oauth/access_token?client_id=' . APPID . '&redirect_uri=' . APPURL . 'callback.php&client_secret=' . APISECRET . '&code=' . $_REQUEST['code']); 

而這正是錯誤報告返回:

[20-Jul-2010 15:47:35] PHP Warning: file_get_contents(https://graph.facebook.com/oauth/access_token?client_id=xxxxxxx&amp;redirect_uri=http://apps.facebook.com/xxxxx/callback.php&amp;client_secret=xxxxxx&amp;code=) [<a href='function.file-get-contents'>function.file-get-contents</a>]: failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request 
在/ home/pk1no /的public_html /視頻/回調

。第8行的PHP

有趣的是,這個腳本在我的Joyent專用上工作,但不在我的HostGator專用上。我有點困惑。

+2

我敢肯定這是那些&字符是導致它,因爲當我把與安培我得到瀏覽器的請求「缺失重定向「(後面是第一個&)。如果我將&更改爲&就可以。 – 2010-07-20 21:06:26

回答

0

在你的機器上啓用了allow_url_fopen?否則,您無法通過URL打開文件。

您也可以嘗試使用urlencode()封裝URL。

+0

是的。我嘗試過使用urlencode()。讓我看看日誌文件在使用時是否會產生不同的錯誤。 – 2010-07-20 21:14:00

+0

urlencode不會很好 - 它會確保它使用的是&,這是不好的。 – 2010-07-20 21:16:27

+0

urlencode不會轉到&您正在考慮htmlentities。 – 2010-07-20 21:18:25

0

這只是獲取HTML實體的錯誤消息,以便在瀏覽器中正確顯示。

你沒有正確地構建你的URL,很可能使它不能被URL打開包裝。使用urlencode包裝任何可能包含一些非URL安全字符...

$url='https://graph.facebook.com/oauth/access_token'. 
    '?client_id=' . urlencode(APPID) . 
    '&redirect_uri=' . urlencode(APPURL) . 'callback.php'. 
    '&client_secret=' . urlencode(APISECRET) . 
    '&code=' . urlencode($_REQUEST['code']); 

$accessToken = file_get_contents($url);