cURL:POST请求被视为GET

时间:2016-02-09 14:30:04

标签: php post curl get

我发了一个POST请求,打印出标题输出信息并注意到它被视为GET。这种行为的原因是什么?

HEADER OUT DATA:

GET /inx/aeGDrYQ HTTP/1.1
Host: example.com
User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3
Accept: */*
Cookie: PHPSESSID=t762fd0nbi12p3hrgb9sgx9k20; ____ri=4485; safemode=1; session=eyJpdiI6Im1HQzlNR1JhMTNDc0JRelYyRVwveUp6N0JxZG56Z2p5K094eSs3YU5HQ3dzPSIsInZhbHVlIjoiVXBPYzN4TVNReURhVnMxQlZ1TndLZ0dYUjltbUVEcW11bkJJMDdMRVZoZ0hHMjRXZ2p6azlcL1FWXC93NnZWN3oreDcxQms3aGlcL3l0MG1vTjd1V21FcmVCVzFnQjVuMUY5dHBWeUlTbU9NSjJcL1d5TlwvTW11ZWp1eHpNd3d4eFZTamV6aThsNldkdlN3aFo0XC9sTnVnU0tXVDRKbWVBU25VU0hJaDREQ1J5M2xDXC9zRUc5OXhWMWJWWG9jYndhczYyZW4xMkUxb3BoU3FmQmMrNVdzM3RqQmgzeHY1NVJ5RXRTNGZOdmQ4dTRCbmRtWVZBN210QVVEVk1BNTFPc1NQcFU3bnd4NEpKbnRaTFliRWNzbkZaXC9YWUF1Nld1ekZSbjVGRXBuZzNoRlBNND0iLCJtYWMiOiI4OWEwNmMyZGVkYjFiYTlmNDY0MDE5MTQwNzE1YzNhYWJjYTA5YjJ3MWMyZjgwMTViN2MyYmI0OWUyNmMwNjM0In0%3D; toastMsg=2; ts1=11e2bb0a86bfb9669c36Xcc407e1e3b3decefcce 

REST代码:

$ch = curl_init('https://example.com/login');

$postData = [
    'name'       => $name,
    'pass'       => $pass
];

$postDataStr = http_build_query($postData);

# Append some fields to the CURL options array to make a POST request. I left out headers, since
# they don't change and added return_transfer for echoing end results
$options[CURLOPT_POST] = 1;
$options[CURLOPT_POSTFIELDS] = $postDataStr;
$options[CURLOPT_HEADER]=1;
$options[CURLOPT_COOKIEJAR]=$cookie;
$options[CURLOPT_USERAGENT]= $useragent;


$options[CURLOPT_FOLLOWLOCATION] = true;
$options[CURLOPT_RETURNTRANSFER] = true;
$options[CURLINFO_HEADER_OUT] = true;

curl_setopt_array($ch, $options);

# Execute
$response = curl_exec($ch);
// echo $response;

$request = curl_getinfo($ch, CURLINFO_HEADER_OUT);
echo "Request sent: $request<br>";

1 个答案:

答案 0 :(得分:1)

您只显示一个请求,我怀疑这是第二个请求,其中第一个是POST,而您在此处看到的GET是在重定向后执行的请求。

curl 可能在遵循基于响应中30x代码的重定向时切换到GET,并且行为由HTTP 1.1规范(RFC 7230和朋友)引导。