【问题标题】:cURL JSON post not working and shows no error PHP 7cURL JSON 帖子不起作用并且没有显示错误 PHP 7
【发布时间】:2016-09-05 15:34:59
【问题描述】:

我有一个奇怪的错误,我在 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 设置为 permissive
  • 我的 cURL 模块确实可以工作,因为我能够从其他来源发布和获取查询
  • localhost:7369 上的程序正在运行。执行 cURL 命令时,它不会在其日志中显示任何传入请求。
  • 我有一个使用 PHP 5 设置的服务器,奇怪的是它确实在那里工作..只是不在我有 PHP 7 的本地开发环境中,所以我不确定是 PHP 7 引起了问题,还是我只是忘了配置一些东西。
  • 我的服务器和我的开发环境都在 CentOS 6.7 上

任何帮助将不胜感激。

【问题讨论】:

  • 你检查过 phpinfo 吗?你的服务器上安装了 curl 吗?如果没有,那么您可以通过 sudo apt-get install php5-curl 模块安装
  • 谢谢,但就像我说的,我有 PHP 7,所以如果我没记错的话,我做了 yum install php70w-common。默认情况下,cURL 包包含在该包中。我已经能够在 cURL 中做一些事情(简单的 POST 和 GET),但不知何故我上面的脚本不起作用。见webtatic.com/packages/php70
  • 禁用您的 iptables 或任何防火墙,然后重试。
  • 尝试使用 telnet localhost 7369 从您的开发环境连接到您的服务,并确保您能够连接
  • 如果 telnet 工作正常,您可以检查debug curl output 作为下一步。这可能会有所帮助。

标签: php json curl php-7


【解决方案1】:

尝试像这样发送 json:

curl_setopt($curl, CURLOPT_POSTFIELDS, $content); 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-02-16
    • 2015-08-05
    • 2021-08-18
    • 2020-07-06
    • 2019-12-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多