【问题标题】:Using post request in curl在 curl 中使用 post 请求
【发布时间】:2017-07-23 16:58:53
【问题描述】:

我在 php 中为发布请求写了几行代码。 在 Firefox 上尝试该请求有效,但不适用于 curl。

  public function action($key, $file){
       $this->ch = curl_init();
       $headers[] = "Accept-Encoding: gzip";
       $headers[] = "Connection: Keep-Alive";
       $headers[] = "Content-Type: application/x-www-form-urlencoded";

       $datas = "arg=test&action=upload";
       curl_setopt($this->ch, CURLOPT_URL, "http://test.com");
       curl_setopt($this->ch, CURLOPT_USERAGENT, $this->agent);
       curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, 1);
       curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, 1);
       curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, false);
       curl_setopt($this->ch, CURLOPT_HTTPHEADER,  $headers);
       curl_setopt($this->ch, CURLOPT_COOKIEFILE, $this->cookie_path);
       curl_setopt($this->ch, CURLOPT_COOKIEJAR, $this->cookie_path);
       curl_setopt($this->ch, CURLOPT_POST, TRUE);
       curl_setopt($this->ch, CURLOPT_POSTFIELDS, $datas);
       curl_setopt($this->ch, CURLINFO_HEADER_OUT, 1);

       $result = json_decode(curl_exec($this->ch));
       echo curl_error($this->ch);
       $info = curl_getinfo($this->ch);
       var_dump($info);
       curl_close($this->ch);
       var_dump($result);
 }

所以我没有错误,但输出的标题如下: GET / HTTP/1.1 用户代理:Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.4) Gecko/20030624 Netscape/7.1 (ax), Mozilla Host: test.com Accept: / 连接:Keep-Alive 内容类型:application/x-www-form-urlencoded

为什么标头使用 GET 而没有 POST ? 你能帮帮我吗?

亲切的问候

【问题讨论】:

    标签: php curl


    【解决方案1】:

    我不确定我是否理解所有问题,但我可以向您展示 cURL 请求和响应的样子。 POST 方法请求不关心用户代理。看看这对你是否有意义。我刚刚将信息发送到我的请求反射器。你可以在这里看到它的实际效果: https://iconoun.com/demo/temp_f42.php

    <?php // demo/temp_f42.php
    /**
     * Working with cURL
     *
     * https://stackoverflow.com/questions/45267667/using-post-request-in-curl#45267667
     */
    error_reporting(E_ALL);
    echo '<pre>';
    
    
    // FROM THE POST AT STACK
    function action($key, $file){
        $this_agent = NULL; // NOT IMPORTANT WITH POST REQUEST
        $this_cookie_path = 'cookie.txt';
    
           $this_ch = curl_init();
           $headers[] = "Accept-Encoding: gzip";
           $headers[] = "Connection: Keep-Alive";
           $headers[] = "Content-Type: application/x-www-form-urlencoded";
    
           $datas = "arg=test&action=upload";
           curl_setopt($this_ch, CURLOPT_URL, "https://iconoun.com/demo/request_reflector.php");
           curl_setopt($this_ch, CURLOPT_USERAGENT, $this_agent);
           curl_setopt($this_ch, CURLOPT_RETURNTRANSFER, 1);
           curl_setopt($this_ch, CURLOPT_FOLLOWLOCATION, 1);
           curl_setopt($this_ch, CURLOPT_SSL_VERIFYPEER, false);
           curl_setopt($this_ch, CURLOPT_HTTPHEADER,  $headers);
           curl_setopt($this_ch, CURLOPT_COOKIEFILE, $this_cookie_path);
           curl_setopt($this_ch, CURLOPT_COOKIEJAR, $this_cookie_path);
           curl_setopt($this_ch, CURLOPT_POST, TRUE);
           curl_setopt($this_ch, CURLOPT_POSTFIELDS, $datas);
           curl_setopt($this_ch, CURLINFO_HEADER_OUT, 1);
    
           $result = curl_exec($this_ch);
           echo curl_error($this_ch);
           $info = curl_getinfo($this_ch);
           var_dump($info);
           curl_close($this_ch);
           var_dump($result);
     }
    
     action(NULL, NULL);

    响应看起来像这样......

    array(27) {
      ["url"]=>
      string(46) "https://iconoun.com/demo/request_reflector.php"
      ["content_type"]=>
      string(24) "text/html; charset=UTF-8"
      ["http_code"]=>
      int(200)
      ["header_size"]=>
      int(271)
      ["request_size"]=>
      int(215)
      ["filetime"]=>
      int(-1)
      ["ssl_verify_result"]=>
      int(0)
      ["redirect_count"]=>
      int(0)
      ["total_time"]=>
      float(0.166215)
      ["namelookup_time"]=>
      float(0.05617)
      ["connect_time"]=>
      float(0.056595)
      ["pretransfer_time"]=>
      float(0.098641)
      ["size_upload"]=>
      float(22)
      ["size_download"]=>
      float(231)
      ["speed_download"]=>
      float(1389)
      ["speed_upload"]=>
      float(132)
      ["download_content_length"]=>
      float(-1)
      ["upload_content_length"]=>
      float(22)
      ["starttransfer_time"]=>
      float(0.159707)
      ["redirect_time"]=>
      float(0)
      ["redirect_url"]=>
      string(0) ""
      ["primary_ip"]=>
      string(12) "69.65.21.212"
      ["certinfo"]=>
      array(0) {
      }
      ["primary_port"]=>
      int(443)
      ["local_ip"]=>
      string(12) "69.65.21.212"
      ["local_port"]=>
      int(58922)
      ["request_header"]=>
      string(193) "POST /demo/request_reflector.php HTTP/1.1
    Host: iconoun.com
    Accept: */*
    Accept-Encoding: gzip
    Connection: Keep-Alive
    Content-Type: application/x-www-form-urlencoded
    Content-Length: 22
    
    "
    }
    string(231) "REQUEST REFLECTED FROM (empty referrer) AT 2017-07-23T12:09:31-05:00
    $_COOKIE: array(0) {
    }
    
    $_GET: array(0) {
    }
    
    $_POST: array(2) {
      ["arg"]=>
      string(4) "test"
      ["action"]=>
      string(6) "upload"
    }
    
    $_FILES: array(0) {
    }
    
    "

    【讨论】:

    • 首先,感谢您的宝贵时间。所以我尝试用你的 url 和我的 url 重现相同的代码。您的返回一个 POST 标头我的 GET 标头!唯一的区别是关于 httpS。所以我切换到 https,现在我还发送了一个 POST 标头。如果您有解释,我很乐意听到,但感谢您,它正在工作
    • 我不知道 - 可能还有其他因素在起作用。在没有肯定的 POST 请求的情况下,cURL 将发出 GET 请求。也许涉及重定向?但这不应该改变请求类型。
    猜你喜欢
    • 1970-01-01
    • 2012-11-19
    • 2015-05-13
    • 1970-01-01
    • 2012-11-15
    • 2017-03-04
    • 2019-01-17
    • 1970-01-01
    • 2021-08-13
    相关资源
    最近更新 更多