Search code examples
phpjsonwebservice-client

How to send data to json webservice?


How can do this curl command with PHP?

curl -i -X GET -u user:userpass --url http://test/statistics.json \
-d '{"period":{"startdate":"YYYY-MM-DD+hh:mm:ss","enddate":"YYYY-MM-DD+hh:mm:ss"}}' \
-H 'Accept: application/json' -H 'Content-type: application/json'

I have tried with this.

$oar = '{"period":{"startdate":"2018-07-06+00:00:00","enddate":"2018-07-07+00:00:00"}}';
$ch = curl_init();

$service_url = $myURL;
//$content= json_decode("{'period':{'startdate':'2018-06-02+00:00:00','enddate':'2018-06-03+00:00:'0'}}");
$curl = curl_init($service_url);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, "fsd:fsdfsdfsdfdf");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_GET, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $oar);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
$curl_response = curl_exec($curl);
$response = json_decode($curl_response);
curl_close($curl);

var_dump($response);
echo $response;

But get a response from the server only GET is accepted. Works if I exclude curl_setopt($curl, CURLOPT_POSTFIELDS, $oar);. But then the service ignore the date range and sets a default range.

-- update

This works fine! Thanks to Ray A!

$service_url = "https://test/urk.json";
$data = array("period" => array("startdate"=>"2018-06-01+00:00:00","enddate"=>"2018-07-07+00:00:00"));
$data_string = json_encode($data);

  $curl= curl_init($service_url);
  curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  curl_setopt($curl, CURLOPT_USERPWD, "dasd:asdas");
  curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "GET");
  curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($curl, CURLOPT_HTTPHEADER, array(
      'Content-Type: application/json',
      'Content-Length: ' . strlen($data_string))
  );
  curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  $result = curl_exec($curl);
  echo $result;

Solution

  • try to follow this code:

        $service_url = $myURL;
        $data = array("period" => array("startdate"=>"2018-07-06+00:00:00","enddate"=>"2018-07-07+00:00:00"));                                                                    
        $data_string = json_encode($data);                                                                                                                                                                                                        
        $curl= curl_init($service_url);  
        curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
        curl_setopt($curl, CURLOPT_USERPWD, "fsd:fsdfsdfsdfdf");                                                                    
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);                                                                  
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);                                                                      
        curl_setopt($curl, CURLOPT_HTTPHEADER, array(                                                                          
            'Content-Type: application/json',                                                                                
            'Content-Length: ' . strlen($data_string))                                                                       
        );                                                                                                                   
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);                                                                                                           
        $result = curl_exec($curl);