cURL无法通过JSON进行post,也没有错误显示 PHP 7

3

我遇到了一个奇怪的错误,在我的PHP 7 cURL脚本中似乎无法正常工作,也没有报错。我的代码:

$content = json_encode(array(
    'I was just testing',
    'Whether or not this is working',
        ));

$collatex_url = 'http://localhost:7369/collate';
$collatex_headers = array(
    "Content-type: application/json; charset=UTF-8;",
    "Content-Length: " . strlen($content),
    "Accept: application/json"
);

$curl = curl_init($collatex_url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $collatex_headers);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_VERBOSE, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, array('json' => $content));
//curl_setopt($curl, CURLOPT_POSTFIELDS, array('json=' . urlencode($content))); //tried, also does not work
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($curl);
echo curl_error($curl); //does not produce an error
echo 'THE RESULT IS' . $result; //the result is empty
curl_close($curl);
  • 我的Selinux已经设置成宽容模式。
  • 我的cURL模块能够工作,因为我已经可以从其他来源进行POST和GET查询。
  • 在localhost:7369上运行的程序正在工作。执行cURL命令时,它的日志中没有显示任何传入请求。
  • 我有一个安装了PHP 5的服务器,奇怪的是它可以正常运行,但是在我的本地开发环境中却不能正常运行。我使用的是PHP 7,因此我不确定是PHP 7引起了问题,还是我忘记配置了某些内容。
  • 我的服务器和开发环境都是在CentOS 6.7上。

非常感谢您的帮助。


你检查过phpinfo了吗?你的服务器上是否安装了curl?如果没有,你可以通过sudo apt-get install php5-curl模块进行安装。 - Mukesh Ram
谢谢,但是像我说的,我有PHP 7,所以如果我没记错的话,我安装了yum install php70w-common。cURL包默认包含在该软件包中。我已经能够让一些cURL工作(简单的POST和GET),但不知何故我的上面的脚本不起作用。请参见https://webtatic.com/packages/php70/。 - Arent
禁用您的iptables或任何防火墙,然后再尝试一次。 - Felippe Duarte
尝试使用telnet localhost 7369从您的开发环境连接到您的服务,并确保您能够连接。 - Tudor Constantin
如果telnet正常工作,您可以进行下一步检查debug curl output。这可能会有所帮助。 - Andrew
1个回答

2

尝试通过以下方式发送JSON:

curl_setopt($curl, CURLOPT_POSTFIELDS, $content); 

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接