【问题标题】:PHP cURL Post request not workingPHP cURL Post请求不起作用
【发布时间】:2015-01-30 12:00:13
【问题描述】:

我在运行正确的 cURL 请求时遇到问题。我打算对 URL 发出 Post 请求。

该示例仅运行命令行 cURL 请求

$ curl -i -X POST {URL}

我正在运行以下代码并且收到“400 Bad Request”的问题

$ch = curl_init();
curl_setopt( $ch, CURLOPT_POST, 1 );
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_TIMEOUT, 10 );
$output = curl_exec( $ch );
curl_close( $ch );

谁能帮助正确发送请求。

【问题讨论】:

    标签: php curl


    【解决方案1】:

    您缺少想要通过请求获得的CURLOPT_POSTFIELDS。 正如here 解释的那样,您需要设置这些字段:

    <?php
    //
    // A very simple PHP example that sends a HTTP POST to a remote site
    //
    
    $ch = curl_init();
    
    curl_setopt($ch, CURLOPT_URL,"http://www.example.com/tester.phtml");
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS,
                "postvar1=value1&postvar2=value2&postvar3=value3");
    
    // in real life you should use something like:
    // curl_setopt($ch, CURLOPT_POSTFIELDS, 
    //          http_build_query(array('postvar1' => 'value1')));
    
    // receive server response ...
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    
    $server_output = curl_exec ($ch);
    
    curl_close ($ch);
    
    // further processing ....
    if ($server_output == "OK") { ... } else { ... }
    
    ?>
    

    【讨论】:

    • 我没有包含CURLOPT_POSTFIELDS,因为我没有任何要发送的字段。一旦我添加了这条线,它就起作用了。
    【解决方案2】:

    试试这个:

    POST 功能:

    function httpPost($url,$params)
    {
      $postData = '';
       //create name value pairs seperated by &
       foreach($params as $k => $v) 
       { 
          $postData .= $k . '='.$v.'&'; 
       }
       rtrim($postData, '&');
    
        $ch = curl_init();  
    
        curl_setopt($ch,CURLOPT_URL,$url);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
        curl_setopt($ch,CURLOPT_HEADER, false); 
        curl_setopt($ch, CURLOPT_POST, count($postData));
            curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);    
    
        $output=curl_exec($ch);
    
        curl_close($ch);
        return $output;
    
    }
    

    调用函数:

    $params = array(
       "name" => "Ravishanker Kusuma",
       "age" => "32",
       "location" => "India"
    );
    
    echo httpPost("http://hayageek.com/examples/php/curl-examples/post.php",$params);
    

    看看,如果这有帮助。参考:link

    【讨论】:

      【解决方案3】:
      curl_setopt( $ch, CURLOPT_POST, TRUE );
      curl_setopt( $ch, CURLOPT_POSTFIELDS, $array );
      

      查看manual

      【讨论】:

        【解决方案4】:

        要发布,只需使用以下 curl 选项并尝试。

        (if your url is - "https") {
          curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
          curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        }
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); //if required.
        curl_setopt($ch, CURLOPT_URL, );
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
        return curl_exec($ch);
        

        我猜你传递的网址没有帖子字段。或者您可以使用 CURLOPT_POSTFIELDS 和您的帖子参数,或者使用 http_build_query(post params) 将其附加到 url http://www.url.com?arg1=val1&arg2=val2

        【讨论】:

          【解决方案5】:

          在同样的代码上花了 5 个小时后,我才找到了我的问题的解决方案

          如果您在 post 字段中使用数组,则必须对该数组进行 json_encode 才能识别为 POST 参数

                  $curl = curl_init();
                  curl_setopt($curl, CURLOPT_URL, $url);
                  curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
                  curl_setopt($curl, CURLOPT_POST, true);
                  curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($fields));
                  curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
                  curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
                 $data = curl_exec($curl);
          

          所以这是带有 POST 请求的工作 CURL 代码 给一个代表+如果我的

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2017-10-18
            • 2019-01-19
            • 1970-01-01
            • 2023-04-06
            • 2019-04-08
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多