CURL设置CURLOPT_POST perminetelly

时间:2014-08-23 12:34:34

标签: php curl

当我使用curl执行多个请求时,我的行为非常奇怪。这是我的功能:

function http_request($curl, $url, $post = null)
{
    echo (isset($post) ? 'POST ' . $url : 'GET ' . $url) . PHP_EOL;

    try
    {
        $cookie_path = tempnam(null, 'b');

        curl_setopt_array($curl, array
        (
            CURLOPT_COOKIEFILE          => $cookie_path,
            CURLOPT_COOKIEJAR           => $cookie_path,
            CURLOPT_RETURNTRANSFER      => true,
            CURLOPT_FOLLOWLOCATION      => true,
            CURLOPT_POST                => count($post),
            CURLOPT_POSTFIELDS          => $post,
            CURLOPT_HEADER              => true,
            CURLOPT_AUTOREFERER         => true,
            CURLOPT_SSL_VERIFYPEER      => false,
            CURLOPT_URL                 => $url,
            CURLOPT_USERAGENT           => 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:30.0) Gecko/20100101 Firefox/30.0',
            CURLOPT_HTTPHEADER          => array
            (
                'Accept-Language: en-US;q=0.6,en;q=0.4',
                'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8'
            )
        ));

        return curl_exec($curl);
    }
    catch(Exception $exception)
    {
        echo $exception;
    }
}

以下是我用来调用它的代码:

$curl = curl_init();
http_request($curl, "http://host.com/url1");
http_request($curl, "http://host.com/url2", "postdata=123");
http_request($curl, "http://host.com/url3");
http_request($curl, "http://host.com/url4");
curl_close($curl);

这是我得到的输出:

GET  http://host.com/url1
POST http://host.com/url2
GET  http://host.com/url3
GET  http://host.com/url4

到目前为止一直很好,但是使用数据包分析器(wireshark),输出看起来像这样:

POST http://host.com/url1
Content-Length: 0;

POST http://host.com/url2
Content-Length: 12;

POST http://host.com/url3
Content-Length: 0;

POST http://host.com/url4
Content-Length: 0;

然后我重写了这样的代码:

function http_request($curl, $url, $post = null)
{
    echo (isset($post) ? 'POST ' . $url : 'GET ' . $url) . PHP_EOL;

    try
    {
        $cookie_path = tempnam(null, 'b');

        curl_setopt_array($curl, array
        (
            CURLOPT_COOKIEFILE          => $cookie_path,
            CURLOPT_COOKIEJAR           => $cookie_path,
            CURLOPT_COOKIESESSION       => true,
            CURLOPT_RETURNTRANSFER      => true,
            CURLOPT_FOLLOWLOCATION      => true,
            CURLOPT_HEADER              => true,
            CURLOPT_AUTOREFERER         => true,
            CURLOPT_SSL_VERIFYPEER      => false,
            CURLOPT_URL                 => $url,
            CURLOPT_USERAGENT           => 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:30.0) Gecko/20100101 Firefox/30.0',
            CURLOPT_HTTPHEADER          => array
            (
                'Accept-Language: en-US;q=0.6,en;q=0.4',
                'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8'
            )
        ));

        if($post != null)
        {
            curl_setopt($curl, CURLOPT_POST, true);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
        }
        else
        {
            curl_setopt($curl, CURLOPT_POST, false);
            curl_setopt($curl, CURLOPT_POSTFIELDS, null);
        }

        return curl_exec($curl);
    }
    catch(Exception $exception)
    {
        echo $exception;
    }
}

但是如果我从函数中删除此代码仍然会发生同样的事情:

        if($post != null)
        {
            curl_setopt($curl, CURLOPT_POST, true);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
        }
        else
        {
            curl_setopt($curl, CURLOPT_POST, false);
            curl_setopt($curl, CURLOPT_POSTFIELDS, null);
        }

在数据包分析器中,我得到:

GET  http://host.com/url1
GET  http://host.com/url2
GET  http://host.com/url3
GET  http://host.com/url4

在第一个请求中如何将其设置为POST是没有意义的,即使我的$ post参数未设置也是如此。谢谢!

1 个答案:

答案 0 :(得分:0)

我依稀记得前段时间我遇到过同样的问题。尝试明确设置CURLOPT_HTTPGET

    if($post != null)
    { 
        curl_setopt($curl, CURLOPT_HTTPGET, false);
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
    }
    else
    {
        curl_setopt($curl, CURLOPT_HTTPGET, true);
        curl_setopt($curl, CURLOPT_POST, false);
        curl_setopt($curl, CURLOPT_POSTFIELDS, null);
    }