简体   繁体   English

PHP cURL Post 请求不起作用

[英]PHP cURL Post request not working

I having issues running the correct cURL request.我在运行正确的 cURL 请求时遇到问题。 I am meant to do a Post request to the URL.我打算对 URL 进行 Post 请求。

The example only runs a command line cURL request该示例仅运行命令行 cURL 请求

$ curl -i -X POST {URL}

The issue I am running the following code and I am getting '400 Bad Request'我正在运行以下代码并且收到“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 );

Can anyone help with sending the request correctly.任何人都可以帮助正确发送请求。

You're missing the CURLOPT_POSTFIELDS you wanna get by the request.您缺少想要通过请求获得的CURLOPT_POSTFIELDS As explained here you're gonna need to set those fields:如此处所述您将需要设置这些字段:

<?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 { ... }

?>

Try this:尝试这个:

POST Function: 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;

}

Calling the Function:调用函数:

$params = array(
   "name" => "Ravishanker Kusuma",
   "age" => "32",
   "location" => "India"
);

echo httpPost("http://hayageek.com/examples/php/curl-examples/post.php",$params);

See, if that helps.看看,如果这有帮助。 Ref: link参考:链接

curl_setopt( $ch, CURLOPT_POST, TRUE );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $array );

Check the manual检查手册

To post, just make use of following curl options and try.要发布,只需使用以下 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);

I guess the url you are passing doesn't have post fields.我猜你传递的 url 没有 post 字段。 eiter you can make use of CURLOPT_POSTFIELDS and your post params their or else attache it to the url using http_build_query(post params) http://www.url.com?arg1=val1&arg2=val2您可以使用 CURLOPT_POSTFIELDS 和您的帖子参数,或者使用 http_build_query(post params) http://www.url.com?arg1=val1&arg2=val2将其附加到网址

Also after spending 5 hours at same code i just found the solution of my problem同样在相同的代码上花了 5 个小时后,我才找到了我的问题的解决方案

If you are using Array in post Fields you have to json_encode that array to recognize as POST parameters如果您在 post 字段中使用 Array,则必须对该数组进行 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);

So This is Working CURL Code WITH POST Request Give a Rep+ If my所以这是带有 POST 请求的工作 CURL 代码,如果我的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM