【问题标题】:PHP Post JSON With CURLPHP 使用 CURL 发布 JSON
【发布时间】:2020-12-09 07:42:10
【问题描述】:

我正在尝试使用 CURL 发布 JSON,并创建了一个表来存储数据以及从我将数据发送到的 url 获得的响应。

但是,回复一直说我的“发布数据为空”,我不知道为什么。

我在https://reqbin.com/ 之类的网站上使用存储在我的表中的数据对其进行了测试,它运行良好,因为我得到了“成功”的响应。

谁能帮我弄清楚为什么我的数据一直被清空?

下面是我的代码

$url = "url that im trying to post Json to.";
$ch = curl_init($url);

curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($json_object));
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

$result = curl_exec($ch);

我也尝试过使用 http_build_query 但它不起作用。

提前致谢!

【问题讨论】:

    标签: php json api curl post


    【解决方案1】:

    PHP默认识别application/x-www-form-urlencoded标准数据类型

    // 一个

    $post = $GLOBALS['HTTP_RAW_POST_DATA'];
    

    // 两个

    $post = file_get_contents("php://input");
    

    【讨论】:

    • 你能说得更具体些吗? url 正在使用 file_get_content("php:/;input") 接收数据
    • 你的 CURL 请求头包含 Content-Type: application/json ,所以你需要使用 file_get_contents("php://input") 来获取请求数据` $post = file_get_contents("php: //输入”); $post = json_decode($post); `
    猜你喜欢
    • 2011-12-19
    • 1970-01-01
    • 1970-01-01
    • 2016-08-10
    • 1970-01-01
    • 2011-03-31
    • 2010-10-23
    • 2012-06-20
    相关资源
    最近更新 更多