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


here code 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 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 return 'none'. missing something?

the actual problem 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 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 sending data raw json in body, not populate $_post variable

hope you


Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

Cross-Compiling Linux Kernel for Raspberry Pi - ${CCPREFIX}gcc -v does not work -

java.lang.NoClassDefFoundError When Creating New Android Project -