PHP cURL Post 请求无效
PHP cURL Post request not working
我有问题 运行 正确的 cURL 请求。我打算向 URL.
发出 Post 请求
示例只运行一个命令行 cURL request
$ curl -i -X POST {URL}
我的问题是 运行 以下代码,我收到“400 错误请求”
$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 );
任何人都可以帮助正确发送请求。
curl_setopt( $ch, CURLOPT_POST, TRUE );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $array );
勾选manual
您错过了您希望通过请求获得的 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 { ... }
?>
试试这个:
POST Function:
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);
看看有没有帮助。参考:link
至post,只需使用以下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);
我想您传递的 url 没有 post 字段。您可以使用 CURLOPT_POSTFIELDS 和您的 post 参数,或者使用 http_build_query(post 参数将其附加到 url
http://www.url.com?arg1=val1&arg2=val2
同样的代码花了 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 代码
给代表+如果我的
我有问题 运行 正确的 cURL 请求。我打算向 URL.
发出 Post 请求示例只运行一个命令行 cURL request
$ curl -i -X POST {URL}
我的问题是 运行 以下代码,我收到“400 错误请求”
$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 );
任何人都可以帮助正确发送请求。
curl_setopt( $ch, CURLOPT_POST, TRUE );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $array );
勾选manual
您错过了您希望通过请求获得的 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 { ... }
?>
试试这个:
POST Function:
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);
看看有没有帮助。参考:link
至post,只需使用以下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);
我想您传递的 url 没有 post 字段。您可以使用 CURLOPT_POSTFIELDS 和您的 post 参数,或者使用 http_build_query(post 参数将其附加到 url http://www.url.com?arg1=val1&arg2=val2
同样的代码花了 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 代码 给代表+如果我的