【发布时间】:2011-02-16 22:02:04
【问题描述】:
我的 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
【问题讨论】:
-
您是否收到错误消息?它在什么方面不起作用?
-
php 没有错误。我检查了 /var/logs 但我不知道要检查哪个日志:(
-
我发现,每当我的 curlopt_httpheader 数组中有项目时,帖子就会空出来。当数组为空时,我的帖子有效。奇怪..有什么想法吗?
-
php curl post json的可能重复