Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to post data using curl and get reponse based on posted data

Tags:

php

curl

Here is my code to POST data:

<?php
$data = array("account" => "1234", "dob" => "30051987", "site" => "mytestsite.com");
$data_string = json_encode($data);

$url = 'http://mydomain.com/curl.php';

$ch = curl_init($url);
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))
);
$result = curl_exec($ch);
curl_close($ch);
$json_result = json_decode($result, true);
?>

<p>Your confirmation number is: <strong><?php echo $json_result['ConfirmationCode']; ?></strong></p>

Whereas on domain/server curl.php file code as under:

<?php
// header 
header("content-type: application/json");

if($_POST):
    echo json_encode(array('ConfirmationCode' => 'somecode'));
else:
    echo json_encode(array('ConfirmationCode' => 'none'));
endif;
?>

But it always return 'none'. Am I missing something?

like image 741
atif Avatar asked Mar 13 '13 04:03

atif


3 Answers

The actual problem is in grabbing it..

<?php
 $data = array("account" => "1234", "dob" => "30051987", "site" => "mytestsite.com");
 $data_string = json_encode($data);

 $url = 'http://mydomain.com/curl.php';

 $ch = curl_init($url);
  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))
   );
  $result = curl_exec($ch);
  curl_close($ch);

  echo $result;

  //$json_result = json_decode($result, true);
 ?>

your code for curl.php

<?php
 $fp = fopen('php://input', 'r');
 $rawData = stream_get_contents($fp);

 echo "<pre>";
 print_r($rawData);
 echo "</pre>";


 /*if($rawData):
 echo json_encode(array('ConfirmationCode' => 'somecode'));;
 else:
 echo json_encode(array('ConfirmationCode' => 'none'));
 endif;*/

 ?>

Since You are sending the data as raw JSON in the body, it will not populate the $_POST variable

Hope this will help you

like image 52
alwaysLearn Avatar answered Oct 13 '22 16:10

alwaysLearn


The function on this link will work.

    <?php

function post_to_url($url, $data) {
    $fields = '';
    foreach ($data as $key => $value) {
        $fields .= $key . '=' . $value . '&';
    }
    rtrim($fields, '&');

    $post = curl_init();

    curl_setopt($post, CURLOPT_URL, $url);
    curl_setopt($post, CURLOPT_POST, count($data));
    curl_setopt($post, CURLOPT_POSTFIELDS, $fields);
    curl_setopt($post, CURLOPT_RETURNTRANSFER, 1);

    $result = curl_exec($post);

    curl_close($post);
    return $result;
}

$data = array(
    "name" => "new Name",
    "website" => "http://bavotasan.com",
    "twitterID" => "bavotasan"
);

$surl = 'http://mydomain.com/curl.php';
echo post_to_url($surl, $data);
?>

Whereas, on curl.php

<?php
file_put_contents('abc.text', "here is my data " . implode('->', $_POST));
if ($_POST['name']):
    print_r($_POST['name']);
endif;
?>
like image 5
atif Avatar answered Oct 13 '22 14:10

atif


The "correct" Content-Type header for a POST request should be application/x-www-form-urlencoded, but you overrode it with application/json (which is only needed from the server side).

Change your code to below:

<?php
$data = array("account" => "1234", "dob" => "30051987", "site" => "mytestsite.com");
$data_string = json_encode($data);

$url = 'http://mydomain.com/curl.php';

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, true); // no need to use custom request method

// ----- METHOD #1: no need to "stringify"
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_array);
// ----- METHOD #2: or if you really like to JOSN-ify
curl_setopt($ch, CURLOPT_POSTFIELDS, array("json"=>$data_string));

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
/* ------ this will be handled by PHP/cURL
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'Content-Length: ' . strlen($data_string))
);
*/
$result = curl_exec($ch);
curl_close($ch);
$json_result = json_decode($result, true);
?>
like image 2
Passerby Avatar answered Oct 13 '22 14:10

Passerby