簡體   English   中英

PHP curl問題 - 如果使用變量作為URL,則出錯,但字符串工作正常

[英]PHP curl issue - error if using a variable as the URL, but a string works fine

我在php中有一個非常奇怪的卷曲問題。 當我將URL作為變量傳入時,請求將返回400錯誤,說“錯誤請求 - 缺少商品ID cookie”。

但是如果將URL硬編碼到代碼中而不是傳遞變量,它就可以正常工作!

這是我的代碼 -

            function resolveURL($url) {

                $ch = curl_init("$url");  
                echo var_dump("$url");

                curl_setopt($ch, CURLOPT_HEADER, 1); 
                curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0); 
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
                $results = curl_exec($ch); 
                curl_close($ch); 

                echo var_dump($results);
            }

$ url的var_dump返回 - string(104) "http://click.linksynergy.com/fs-bin/click?id=SN4dmHdm/i8&offerid=462146.45&type=3&subid=0"

$ results的var_dump返回

string(319) "HTTP/1.1 400 Bad Request Server: Apache-Coyote/1.1 Content-Length: 187 Date: Thu, 22 Dec 2016 14:14:38 GMT Connection: close Missing offer id cookie "

但如果我對上面的代碼進行一處更改 -

$ch = curl_init("http://click.linksynergy.com/fs-bin/click?id=SN4dmHdm/i8&offerid=462146.45&type=3&subid=0");

然后$ results返回我期望的完整數據 -

string(1125) "HTTP/1.1 302 Found Server: Apache-Coyote/1.1 Set-Cookie: lsn_statp=SSUMBwoAAAAlJc5KdQpJiw%3D%3D; Domain=.linksynergy.com; Expires=Wed, 17-Dec-2036 14:16:14 GMT; Path=/ Set-Cookie: rmuid=e431e402-a482-4611-a78a-71aa74f859f5; Domain=.linksynergy.com; Expires=Fri, 22-Dec-2017 14:16:14 GMT; Path=/ Set-Cookie: lsn_qstring=SN4dmHdm%2Fi8%3A462146%3A; Domain=.linksynergy.com; Expires=Fri, 23-Dec-2016 14:16:14 GMT; Path=/ Set-Cookie: lsn_track=UmFuZG9tSVZDI%2Bw8hSY%2BdfkMlwCUwBvtWwSdxm0SOqHNgxhyQqiQeQ0nwzwxomWBIzUbWVW%2Ft7lFTQ3k3hZXdQ%3D%3D; Domain=.linksynergy.com; Expires=Sun, 20-Dec-2026 14:16:14 GMT; Path=/ Set-Cookie: lsclick_mid38366="2016-12-22 14:16:14.418|SN4dmHdm_i8-R8CHpEfhXw637RnHirbegw"; Version=1; Domain=.linksynergy.com; Max-Age=63072000; Expires=Sat, 22-Dec-2018 14:16:14 GMT; Path=/ P3P: policyref="/w3c/p3p.xml", CP="NOI DSP COR CURa ADMa DEVa OUR BUS STA" Expires: Thu, 01 Jan 1970 00:00:00 GMT Date: Thu, 22 Dec 2016 14:16:14 GMT Cache-Control: no-cache Pragma: no-cache Location: http://www.daskeyboard.com/?siteID=SN4dmHdm_i8-R8CHpEfhXw637RnHirbegw Content-Length: 0 Connection: close "

為什么會這樣? 我想出於某種原因,當我將URL作為變量留下時,第一個&之后的一切都被剝離了,但我無法解釋原因。

我試過通過urlencode()htmlentities()htmlspecialchars()運行$ url,但我仍然得到相同的結果。 我試過傳入$url而不是"$url" ,並沒有解決問題。

更新:當我做一個echo var_dump(curl_getinfo($ch)); 在設置URL之后,看起來字符串的長度在某種程度上比傳入$url時應該更長,但在使用硬編碼字符串時是正確的長度。 結果如下 -

//$url
`array(21) { ["url"]=> string(104) "http://click.linksynergy.com/fs-bin/click?id=SN4dmHdm/i8&offerid=462146.45&type=3&subid=0" ["content_type"]=> NULL ["http_code"]=> int(0) ["header_size"]=> int(0) ["request_size"]=> int(0) ["filetime"]=> int(0) ["ssl_verify_result"]=> int(0) ["redirect_count"]=> int(0) ["total_time"]=> float(0) ["namelookup_time"]=> float(0) ["connect_time"]=> float(0) ["pretransfer_time"]=> float(0) ["size_upload"]=> float(0) ["size_download"]=> float(0) ["speed_download"]=> float(0) ["speed_upload"]=> float(0) ["download_content_length"]=> float(-1) ["upload_content_length"]=> float(-1) ["starttransfer_time"]=> float(0) ["redirect_time"]=> float(0) ["certinfo"]=> array(0) { } } `

//hard-coded
`array(21) { ["url"]=> string(89) "http://click.linksynergy.com/fs-bin/click?id=SN4dmHdm/i8&offerid=462146.45&type=3&subid=0" ["content_type"]=> NULL ["http_code"]=> int(0) ["header_size"]=> int(0) ["request_size"]=> int(0) ["filetime"]=> int(0) ["ssl_verify_result"]=> int(0) ["redirect_count"]=> int(0) ["total_time"]=> float(0) ["namelookup_time"]=> float(0) ["connect_time"]=> float(0) ["pretransfer_time"]=> float(0) ["size_upload"]=> float(0) ["size_download"]=> float(0) ["speed_download"]=> float(0) ["speed_upload"]=> float(0) ["download_content_length"]=> float(-1) ["upload_content_length"]=> float(-1) ["starttransfer_time"]=> float(0) ["redirect_time"]=> float(0) ["certinfo"]=> array(0) { } } `

任何人都可以解釋當我使用變量時額外的15個字符來自哪里?

更新:通過在$url上執行str_split ,我發現由於某種原因,有一個隱藏的'#038;' 在每個&之后的變量中,當我回顯字符串時沒有顯示。 下面是數組返回的內容 -

[56]=> string(1) "&" [57]=> string(1) "#" [58]=> string(1) "0" [59]=> string(1) "3" [60]=> string(1) "8" [61]=> string(1) ";"

以下是修復它的原因..

$url= str_replace("#038;", "", $url);

嘗試像這樣組裝:

$ch = curl_init();
   curl_setopt_array($ch, array(
   CURLOPT_RETURNTRANSFER => 1,
   CURLOPT_URL => $url,
   (the rest of your setopt array)
));

更多信息

您需要對請求字符串的不同部分使用不同的編碼。

對URL使用rawurlencode(),然后使用urlencode()為每個參數構造請求字符串:

$url = rawurlencode("http://click.linksynergy.com/fs-bin/click");

$params_urlencoded = array_map("urlencode", $params);
$getfields = "id=".$params_urlencoded['id']
            ."&offerid=".$params_urlencoded['offerid']
            ."&type=".$params_urlencoded['type']
            ."&subid=".$params_urlencoded['subid'];

$ch = curl_init($url.'?'.$getfields);

看起來你的&符號分隔參數名稱被編碼為特殊字符( & ),因此在服務器上解析時會出現更多的符號和錯誤的變量名。

另外,閱讀文檔:)

B.2.2 URI屬性值中的&符號

php urlencode

php rawurlencode

故事的寓意 - 我假設因為我在瀏覽器中調試,我無法看到URL中的&符編碼為"#038;" 即使我是從PHP回應。

顯然,curl不適用於這些,因此解決方法是將它們從URL變量中剝離 -

$url = str_replace("#038;", "", $url);

后面的URL可以根據需要進行編碼。

暫無
暫無

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

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