php – Curl POST作为GET执行

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – Curl POST作为GET执行脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试使用 PHP开发一种浏览器.
到目前为止,我的班级可以使用以下内容类型处理GET或POST请求:application / x-www-form-urlencoded.

现在我需要转向JSON.我已将Content-tyPE标头设置为application / json.

事实是,使用这种类型我遇到了以下问题:设置POST请求将导致GET请求.这真的很奇怪.

这是我的代码

PRivate function request($url,$reset_cookies,$post_data = null,$custom_headers = null)
{       
    // Create options
    $options = array(
        CURLOPT_URL               => $url,CURLOPT_RETURNtransfer    => 1,CURLOPT_HEADER            => 0,CURLINFO_HEADER_OUT       => 1,CURLOPT_FaiLONERROR       => 1,CURLOPT_USERAGENT         => $this->user_agent,CURLOPT_CONNECTTIMEOUT    => 30,CURLOPT_TIMEOUT           => 30,CURLOPT_FOLLOWLOCATION    => 1,CURLOPT_MAXredIRS         => 10,CURLOPT_AUTOREFERER       => 1,CURLOPT_COOKIESESSION     => $reset_cookies ? 1 : 0,CURLOPT_cookiejar         => $this->cookies_file,CURLOPT_COOKIEFILE        => $this->cookies_file,CURLOPT_HTTPHEADER        => array('Accept-language: en'),// SSL
        /*
        CURLOPT_SSL_CIPHER_LIST   => 'TLSv1',CURLOPT_SSL_VERIFYPEER    => 1,CURLOPT_CAINFO            => dirname(__FILE__) . '/Entrust.netCertificationAuthorITy(2048).crt',*/
    );

    // Add headers
    if (isset($custom_headers)) $options[CURLOPT_HTTPHEADER] = array_merge($options[CURLOPT_HTTPHEADER],$custom_headers);

    // Add POST data
    if (isset($post_data))
    {
        $options[CURLOPT_POST]          = 1;
        $options[CURLOPT_POSTFIELDS]    = is_string($post_data) ? $post_data : http_build_query($post_data);
    }

    // Attach options
    curl_setopt_array($this->curl,$options);

    // Execute the request and read the response
    $content = curl_exec($this->curl);

    print_r($options);
    print_r(curl_getinfo($this->curl,CURLINFO_HEADER_OUT));

    // Clean local VARiables
    unset($url);
    unset($reset_cookies);
    unset($post_data);
    unset($custom_headers);
    unset($options);

    // Handle any error
    if (curl_errno($this->curl))
    {
        unset($content);
        throw new Exception(curl_error($this->curl));
    }

    return $content;
}

为了说明我的问题,这是一个例子:

CUrl选项作为数组:

Array
(
    [10002] => http://mywebsite.COM/post/
    [19913] => 1
    [42] => 0
    [2] => 1
    [45] => 1
    [10018] => Mozilla/5.0 (Windows NT 6.1; WOW64; rv:10.0.2) Gecko/20100101 Firefox/10.0.2
    [78] => 30
    [13] => 30
    [52] => 1
    [68] => 10
    [58] => 1
    [96] => 0
    [10082] => C:\wamp\www\v2\_libs/../_cookies/14d0fd2b-9F15-4ac5-8fae-4246cc6cef49.cookie
    [10031] => C:\wamp\www\v2\_libs/../_cookies/14d0fd2b-9f15-4ac5-8fae-4246cc6cef49.cookie
    [10023] => Array
        (
            [0] => Accept-language: en
            [1] => RequestVerificationToken: 4PMxvJsQzFJ5oFt3JdUPe6Bp_geij4obDJCYIRoU09PrrfcBSUgJT9iB3mXnGFc2KSlYrPcRHF7iHdQhGNu0GKLUzd5FywfaADbGS8wjhXraF36W0
            [2] => Content-Type: application/json
        )

    [47] => 1
    [10015] => {"usernameOrFeedId":"manitoba","Feed_message_body":"Dummy message goes here"}
)

所以请求标题对我来说似乎很好,但我可能错了.

这是CUrl发送的真实标题

GET /post/ HTTP/1.1
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:10.0.2) Gecko/20100101 Firefox/10.0.2
Host: mywebsite.com
Accept: */*
Referer: http://mywebsite.com/post/
Cookie: ADRUM_BT=R%3a53%7cclientRequestGUID%3a9787a51b-b24d-4400-9d6a-efbd618c74c0%7cbtId%3a18790%7cbtERT%3a44; CSRFToken=o_eoIVji7pWclOsrLaJpZEbOFSBJBm851rHbH0Xqwdzw2tC5j07EAc23MLj-opWowgpj0RkHyiktl1cS6onBqI43afM1; WebSessionId=3aem0m2xpwmvesgphna5gaop; prod=rd101o00000000000000000000ffff0a5a2a74o80; AuthenticateCookie=AAAAAtsQgeb8+UXrJ+wa7CGVJKnqizEAo2bMuFvqvwYMAl1NRaa6z68Lbrx9hiHzPBC8tYqiayHID6pHChGXB7VywemwTpGivcRQ3nRlUVuaYQKyxQt21p1mx7OMlLCsRA==; web_lang.prod=fr
Accept-language: en
RequestVerificationToken: 4PMxvJsQzFJ5oFt3JdUPe6Bp_geIj4obDJCYIRoU09PrrfcBSUgJT9iB3mXnGFc2KSlYrPcRHF7iHdQhGNu0GKLUzd5FywfaADbGS8wjhXraF34W0
Content-Type: application/json

正如您所看到的,这是一个GET请求,而后期数据看起来已经消失了.

我做错了吗?

解决方法

您正在关注重定向,这意味着您获得了3xx响应代码,curl向新URL发出第二个请求.

curl将根据特定的3xx代码执行操作,对于某些重定向,它会将请求方法从POST更改为GET – 启用VERBOSE将显示是否这样做.使卷曲更改方法的响应代码301,302和303.这样做是因为浏览器如何处理这些响应代码.

libcurl提供了一个名为CURLOPT_POSTREDIR的选项,您可以使用该选项告诉curl不要更改特定HTTP响应的方法.使用它,即使在使用这些响应代码之一重定向后,您也可以使用curl发送POST.

脚本宝典总结

以上是脚本宝典为你收集整理的php – Curl POST作为GET执行全部内容,希望文章能够帮你解决php – Curl POST作为GET执行所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。