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
curl_close($curl);
  • 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.

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

  $ 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); 
  code>  pre> 
 
 
  • 我的Selinux设置为允许 li>
  • 我的cURL模块确实有效,因为我已经能够从其他来源发出POST和GET查询 li>
  • localhost:7369上的程序正在运行并正在运行。 执行cURL命令时,它不会在其日志中显示任何incomming请求。 li>
  • 我有一个使用PHP 5设置的服务器,奇怪的是它确实在那里工作..不是在我有PHP 7的本地开发环境中,所以我不确定PHP 7是否导致 问题,或者我只是忘了配置一些东西。 li>
  • 我的服务器和我的开发环境都在CentOS 6.7上。 li> ul>

    任何帮助都将不胜感激。 p> div>

try by sending the json like this:

curl_setopt($curl, CURLOPT_POSTFIELDS, $content);