我的POST curl可以从命令行运行,但不能从php运行。它不是用PHP发送POST数据。 (我已经检查过它是否正在重写为GET,如果我使用GET,那么就不会这样做GET也能正常工作)
命令行:
curl -d "something=true" file.php
PHP:
error_reporting(E_ALL);
$ch = curl_init();
$post = 'something=true';
$arr = array();
array_push($arr, 'Accept: application/json, text/javascript, */*; q=0.01');
array_push($arr, 'Accept-Language: en-us,en;q=0.5');
array_push($arr, 'Accept-Encoding=gzip,deflate');
array_push($arr, 'Accept-Charset=ISO-8859-1,utf-8;q=0.7,*;q=0.7');
array_push($arr, 'Keep-Alive: 115');
array_push($arr, 'Connection: keep-alive');
array_push($arr, 'Content-Type: application/json; charset=utf-8');
array_push($arr, 'x-request-with: XMLHttpRequest');
array_push($arr, 'Content-Length: ' . strlen($post));
curl_setopt($ch, CURLOPT_HTTPHEADER, $arr);
curl_setopt($ch, CURLOPT_URL, 'http://mydomain.com/file.php');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13');
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_exec($ch);
请参阅: php curl post json
答案 0 :(得分:0)
不要自己设置Content-Length,但允许libcurl自行完成,以减少出错的风险。
然后,你添加了两个没有冒号的标题,改为使用'=',这可能会使接收端感到困惑。
答案 1 :(得分:0)
了解如何使用curl发送帖子数据:
function api_send($params,$token,$backup = false)
{
static $content;
if ($backup == true) {
$url = 'http://app.x/api.php';
} else {
$url = 'http://app.x/api.php';
}
$c = curl_init();
curl_setopt($c, CURLOPT_URL, $url);
curl_setopt($c, CURLOPT_POST, true);
curl_setopt($c, CURLOPT_POSTFIELDS, $params);
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
// Headers here
curl_setopt($c, CURLOPT_HTTPHEADER, array(
"Authorization: Bearer $token"
));
// Disable ssl check
// curl_setopt($c, CURLOPT_SSL_VERIFYPEER => false);
// curl_setopt($c, CURLOPT_SSL_VERIFYHOST => false);
// Ssl version
// curl_setopt($c, CURLOPT_SSLVERSION => 3);
$content = curl_exec($c);
$http_status = curl_getinfo($c, CURLINFO_HTTP_CODE);
if ($http_status != 200 && $backup == false) {
api_send($params, $token, true);
}
curl_close($c);
return $content;
}
示例
$params = array(
'pass' => 'pass',
'message' => 'Message here',
'cmd' => 'sendnow'
);
// Send data
echo api_send($params,'Token_here');
你需要试试!