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);
    ?>
    

    );

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

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料