【发布时间】:2019-12-07 11:48:15
【问题描述】:
我正在尝试使用 PHP 发布 JSON 有效负载,但无法正常工作。我有以下适用于 Shell 的 CURL 命令以及以下也适用的 Python 代码,但需要 PHP 实现相同的功能。
外壳上的 CURL:
curl -H "Content-Type: application/json" -X POST -d '{"Content":" <CONTENT OF THE MESSAGE> "}' <URL of the node receiving POST data>
Python:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import requests
headers = {
'Content-type': 'application/json',
}
auth = '<URL>'
line1 = '<CONTENT of message>'
data = '{"Content":"%s"}' % (line1)
response = requests.post(url = auth, headers = headers, data = data)
到目前为止我在 PHP 中所拥有的(不工作):
$data = array("Content" => "<CONTENT of the message>");
$data_string = json_encode($data);
$ch = curl_init('<URL>');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$result = curl_exec($ch);
提前非常感谢任何和所有帮助!
【问题讨论】:
-
实际的错误消息是什么,或者具体是什么不起作用?
-
它基本上是一个消息应用程序的 webhook...在运行 PHP 文件后,没有任何反应,而其他两个“发布”消息。
-
这能回答你的问题吗? How to POST JSON Data With PHP cURL?
-
请从 curl 命令行中删除
-X POST部分...
标签: php curl post http-post php-curl