1

I am working on API in which i need to make a post call to a url.

body is

{
"image":"http://media.kairos.com/kairos-elizabeth.jpg",
"subject_id":"subtest1",
"gallery_name":"gallerytest1",
"selector":"SETPOSE",
"symmetricFill":"true"
}

header is

 'Content-Type:application/json',
 'app_id:app_id',
 'app_key:app_key'

i am using curl.code is below

<?php
$ch = curl_init('some url');

$content= array(
'image'=>'some_image.jpg',
'subject_id'=>'subtest1',
'gallery_name'=>'gallerytest1',
'selector'=>'SETPOSE',
'symmetricFill'=>'true'
 );

curl_setopt_array($ch, array(
CURLOPT_POST => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_HTTPHEADER => array(
    'Content-Type:application/json',
    'app_id:id',
    'app_key:key'
),
CURLOPT_POSTFIELDS => json_encode($content)
));

$response = curl_exec($ch);

if($response === FALSE){
die(curl_error($ch));
}

$responseData = json_decode($response, TRUE);
?>

When i run the script blank page appears.Help me find the issue.

shubhamj
  • 778
  • 1
  • 7
  • 13

1 Answers1

0

Try my source bro

$data_string = array(
    'image'         =>'some_image.jpg',
    'subject_id'    =>'subtest1',
    'gallery_name'  =>'gallerytest1',
    'selector'      =>'SETPOSE',
    'symmetricFill' =>'true'
 );
$data_string = json_encode($data_string);
$ch = curl_init('http://localhost/testGuzzle/jsOnChange.php');                    
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                                                                  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
    'Content-Type: application/json',                                                      
    'Content-Length: ' . strlen($data_string),
    'app_id:id',
    'app_key:key'
    ));                                                                 
$result = curl_exec($ch);
if($result === FALSE){
    die(curl_error($ch));
}else{
    echo $result;
}

And my listener source

ini_set("allow_url_fopen", true);
$jsonStr = file_get_contents("php://input"); //read the HTTP body.
var_dump($jsonStr);
echo "<br>";
$json = json_decode($jsonStr, true);
print_r($json);

And result

enter image description here

Hope this help!!! xD

Community
  • 1
  • 1
Quynh Nguyen
  • 2,959
  • 2
  • 13
  • 27