2016-05-10 14:58
浏览 46

cURL JSON帖子不起作用,并且没有显示错误PHP 7

I have a strange error in which my cURL script in PHP 7 does not seem to work, and does not give an error. My code:

$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
  • my Selinux is set to permissive
  • my cURL module does work, as Ive been able to POST and GET queries from other sources
  • The program on localhost:7369 is running and working. It does not show any incomming request in its log when executing the cURL command.
  • I have a server set up with PHP 5, and strangely it does work there..just not on my local dev environment on which I have PHP 7, so Im not sure if PHP 7 is causing the problem, or that I just forgot to configure something.
  • Both my server and my dev environment are on CentOS 6.7

Any help would be appreciated.

图片转代码服务由CSDN问答提供 功能建议

我有一个奇怪的错误,其中我的PHP 7中的cURL脚本似乎不起作用,并且没有给出 错误。 我的代码:

  $ content = json_encode(array(
)); \  n 
 $ collat​​ex_url ='http:// localhost:7369 / collat​​e'; 
 $ collat​​ex_headers = array(
“Content-type:application / json; charset = UTF-8;”,
“Content-Length  :“。strlen($ content),
”接受:application / json“
 $ curl = curl_init($ collat​​ex_url); 
curl_setopt($ curl,CURLOPT_HTTPHEADER,$ collat​​ex_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)));  //尝试过,也不起作用
curl_setopt($ curl,CURLOPT_RETURNTRANSFER,true); 
 $ result = curl_exec($ curl); 
echo curl_error($ curl);  //不会产生错误
echo'结果是'。  $结果;  //结果为空
 ncurl_close($ curl); 
  • 我的Selinux设置为允许
  • 我的cURL模块确实有效,因为我已经能够从其他来源发出POST和GET查询
  • localhost:7369上的程序正在运行并正在运行。 执行cURL命令时,它不会在其日志中显示任何incomming请求。
  • 我有一个使用PHP 5设置的服务器,奇怪的是它确实在那里工作..不是在我有PHP 7的本地开发环境中,所以我不确定PHP 7是否导致 问题,或者我只是忘了配置一些东西。
  • 我的服务器和我的开发环境都在CentOS 6.7上。


  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dpmir1988 2016-05-10 18:21

    try by sending the json like this:

    curl_setopt($curl, CURLOPT_POSTFIELDS, $content); 
    解决 无用
    打赏 举报

相关推荐 更多相似问题