Reputation: 135
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
Upvotes: 2
Views: 836
Reputation: 1971
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);
?>
);
Upvotes: 3