donglu7286 2018-08-12 13:13
浏览 237
已采纳

JSON原始POST cURL请求不起作用

any help is very appreciated, thanks in advance.

my storeCallback.php file which i have hosted at: http://xxxxx/storeCallback.php

<?php
die(var_dump($_REQUEST));

and i am creating a post json raw request as:

1.

$url = "http://xxxxx/storeCallback.php/storeCallback.php";

$body =['foo' => 'bar'];

$body_string = json_encode($body);

$header = array(
    'Accept: application/json',
    'Content-Type: application/json',
    'Content-Length: '.strlen($body_string),
);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $body_string);

$json_response = curl_exec($curl);
$response = json_decode($json_response, true);
die(var_dump($response));

2.

$url = "http://xxxxx/storeCallback.php/storeCallback.php";

$body =['foo' => 'bar'];

$body_string = json_encode($body);

$header = array(
    'Accept: application/json',
    'Content-Type: application/json',
    'Content-Length: '.strlen($body_string),
);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $body_string);

$json_response = curl_exec($curl);
$response = json_decode($json_response, true);
die(var_dump($response));

3.

<?php
require 'vendor/autoload.php';

use GuzzleHttp\Client;

$client = new Client();

$response = $client->post('http://xxxxx/storeCallback.php/storeCallback.php', [
    GuzzleHttp\RequestOptions::JSON => ['for' => "bar"],
]);

echo $response->getBody()->getContents();

Nothing is working, in all cases response is [] (empty array) but sending request like http://xxxxx/storeCallback.php/storeCallback.php?foo=bar is working

  • 写回答

1条回答 默认 最新

  • dongsaohu6429 2018-08-12 13:19
    关注

    There are two problems with your code, first of all the storeCallback.php file does not provide valid JSON output, therefore you cannot parse it as JSON when you try to retrieve it using cURL.

    The proper version is the following: storeCallback.php

    <?php
    die(json_encode($_REQUEST));
    ?>
    

    Furthermore, you should be making the cURL request as follows:

    <?php
    $url = "http://pc.medimetry.in/storeCallback.php";
    $body = ['foo' => 'bar'];
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
    
    $json_response = curl_exec($ch);
    curl_close($ch); //important, always close cURL connections.
    $parsed_response = json_decode($json_response);
    var_dump($parsed_response);
    ?>
    

    );

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿