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 ansys fluent计算闪退
  • ¥15 有关wireshark抓包的问题
  • ¥15 需要写计算过程,不要写代码,求解答,数据都在图上
  • ¥15 向数据表用newid方式插入GUID问题
  • ¥15 multisim电路设计
  • ¥20 用keil,写代码解决两个问题,用库函数
  • ¥50 ID中开关量采样信号通道、以及程序流程的设计
  • ¥15 U-Mamba/nnunetv2固定随机数种子
  • ¥15 vba使用jmail发送邮件正文里面怎么加图片
  • ¥15 vb6.0如何向数据库中添加自动生成的字段数据。