php get資料問題請教

來源:互聯網
上載者:User
PC用戶端通過crul post資料至後台:
http://218.204.14.50/test/?oldip=61.141.251.21&newip=61.141.251.25&urldata=http://detail.ju.taobao.com/home.htm?spm=601#/&agent=Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; QQDownload 732; .NET4.0C; .NET4.0E) &normal=100&error=102

後台PHP擷取資料代碼:
$oldip=$_GET["oldip"];
$newip=$_GET["newip"];
$urldata=$_GET["urldata"];
$agent=$_GET["agent"];
$normal=$_GET["normal"];
$error=$_GET["error"];
file_put_contents(test.txt,$oldip,FILE_APPEND);
file_put_contents(test.txt,$newip,FILE_APPEND);
file_put_contents(test.txt,$urldata,FILE_APPEND);
file_put_contents(test.txt,$agent,FILE_APPEND);
file_put_contents(test.txt,$normal,FILE_APPEND);
file_put_contents(test.txt,$error,FILE_APPEND);
?>

後台只能擷取到oldip、newip、urldata資料,擷取不到agent、normal、error資料,請問高手可能是什麼原因?如何解決?
非常感謝!


回複討論(解決方案)

既然是 post,那麼怎麼用 $_GET 接受呢?
要用 $_POST !

$oldip=$_POST["oldip"];
$newip=$_POST["newip"];
$urldata=$_POST["urldata"];
$agent=$_POST["agent"];
$normal=$_POST["normal"];
$error=$_POST["error"];
file_put_contents(test.txt,$oldip,FILE_APPEND);
file_put_contents(test.txt,$newip,FILE_APPEND);
file_put_contents(test.txt,$urldata,FILE_APPEND);
file_put_contents(test.txt,$agent,FILE_APPEND);
file_put_contents(test.txt,$normal,FILE_APPEND);
file_put_contents(test.txt,$error,FILE_APPEND);
?>

使用$_POST之後,oldip、newip、urldata、agent、normal、error變數的資料都收不到了,這是為什麼呢?
謝謝!

你把參數放到 url裡面傳遞了,怎麼能擷取到資料呢!

$_REQUEST
$_POST
$_GET
都 記錄下來,看看值在哪傳的

具體傳值代碼怎麼寫?貼出來瞧瞧

嗯,你實際是 get 方式傳值的
因為有 spm=601 #/
按約定 # 表示錨點,不會傳往 文本伺服器
所以其後的內容被截斷了,所以你接收不到

而用 post 傳值時就不會出現這種情況
貼出你的 curl 代碼

明顯你是用get方式傳遞的。

get 傳遞的參數需要用urlencode轉一次。

http://218.204.14.50/test/?oldip=61.141.251.21&newip=61.141.251.25&urldata=http://detail.ju.taobao.com/home.htm?spm=601#/&agent=Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; QQDownload 732; .NET4.0C; .NET4.0E) &normal=100&error=102

應該改為

echo 'http://218.204.14.50/test/?oldip='.urlencode('61.141.251.21').'&newip='.urlencode('61.141.251.25').'&urldata='.urlencode('http://detail.ju.taobao.com/home.htm?spm=601#/').'&agent='.urlencode('Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; QQDownload 732; .NET4.0C; .NET4.0E) ').'&normal=100&error=102';


http://218.204.14.50/test/?oldip=61.141.251.21&newip=61.141.251.25&urldata=http%3A%2F%2Fdetail.ju.taobao.com%2Fhome.htm%3Fspm%3D601%23%2F&agent=Mozilla%2F4.0+%28compatible%3B+MSIE+6.0%3B+Windows+NT+5.1%3B+SV1%3B+QQDownload+732%3B+.NET4.0C%3B+.NET4.0E%29+&normal=100&error=102

謝謝xuzuning、fdipzone版主的回複
curl代碼:
CURL *curl;
CURLcode res;

curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
if(curl) {
CString url("http://218.204.14.50/test/?oldip=");
url += csoldip;
url += "&newip=";
url += csnewip;
url += "&urldata=";
url += csurldata;
url += "&agent=";
url += csagent;
url += "&normal=";
url += csnormal;
url += "&error=";
url += cserror;

curl_easy_setopt(curl, CURLOPT_URL, CT2A(url));
curl_easy_setopt(curl, CURLOPT_HTTPGET);
res = curl_easy_setopt(curl, CURLOPT_USERAGENT, "tian_test");
res = curl_easy_perform(curl);
if(CURLE_OK == res)
return TRUE;
curl_easy_cleanup(curl);
}
curl_global_cleanup();

用戶端使用urlencode轉碼之後,後台是不是還要解碼?
謝謝!

你這是 C++ 還是 C#
有對應於 php urlencode 的函數嗎?
如果有,則將 形如 url += csoldip; 的
改為形如 url += urlencode(csoldip); 的
url 編碼後,php 端無需解碼

哦,忘了寫,用戶端是C++,我試試。多謝!

urlencode之後還是擷取不到agent、normal、error資料,但將urldata值置為test之後,就可以收到資料了,懷疑是不是$_GET長度有限制,如果不用$_GET、$_REQUEST,還有其它解決方案嗎?謝謝!

get 方式有 2k 的上限
所以你這的該用 post 方式
http://www.baidu.com/s?wd=c%2B%2B+curl+post&ie=utf-8

urlencode之後還是擷取不到agent、normal、error資料,但將urldata值置為test之後,就可以收到資料了,懷疑是不是$_GET長度有限制,如果不用$_GET、$_REQUEST,還有其它解決方案嗎?謝謝!



$_GET有2k的限制,改用POST就好了。

謝謝xuzuning 、fdipzone版主的回複,用戶端改用POST之後,後台基本可以擷取到資料了,但遇到了新的問題,再請教一下:
PC用戶端vc ++ crul post資料至後台:
http://218.204.14.50/test/?oldip=61.141.251.21&newip=61.141.251.25&urldata=http://detail.ju.taobao.com/home.htm?uastk=8c3def7900&userid=301115#/&agent=Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; QQDownload 732; .NET4.0C; .NET4.0E) &normal=100&error=102


後台PHP代碼:
oldip=$_POST['oldip']; //結果:61.141.251.21
newip=$_POST['newip']; //結果:61.141.251.25
agent=$_POST['agent']; //結果:Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; QQDownload 732; .NET4.0C; .NET4.0E)
normal=$_POST['normal']; //結果:100
error=$_POST['error']; //結果:102
以上變數都能正確擷取,但urldata擷取有問題
urldata=$$_POST['urldata']; //希望擷取的內容:http://detail.ju.taobao.com/home.htm?uastk=8c3def7900&userid=301115#/
但實際上擷取的內容:http://detail.ju.taobao.com/home.htm?uastk=8c3def7900,無法擷取&userid=301115#/,可能是&號分割的問題,請問這種情況應該怎麼處理?


file_put_contents('test.txt', print_r($_POST,1));
貼出 test.txt 的內容

使用urlencode已解決,再次感謝2位版主的協助。謝謝!

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.