Mikey Barker
Mikey Barker

Reputation: 83

Post JSON data to an API via PHP

I need to integrate JSON into PHP, more specifically I need to post some information from a php page on our site to initiate an API request which will then send me information back. The information I am sending revolves around login information and I am struggling to work out how I can send this information.

This is the information I need to post:

POST /user/?action=login HTTP/1.1
Host: api.interlinkexpress.com
Content-Type: application/json
Accept: application/json
Authorization: Basic RFNNSVRIOk1ZUEFTU1dE
GEOClient: account/123456
Content-Length: 0

Reading around the subject I have seen that php cURL may be useful however the example I have doesn't contain all the information I need to pass over. Also I am unsure on whether I need to pass it via a button as well:

      $data = array("username" => "TESTAPI", "password" => "APITEST");                                                                    
        $data_string = json_encode($data);                                                                                   

        $ch = curl_init('http://api.interlinkexpress.com');                                                                      
        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);

Any help on this matter would be appreciated. I am new to JSON having only learnt about it fleetingly.

edit: I suppose the main thing I'm asking is how do I pass this information to the API in PHP?

 POST /user/?action=login HTTP/1.1
 Host: api.interlinkexpress.com
 Content-Type: application/json
 Accept: application/json
 Authorization: Basic RFNNSVRIOk1ZUEFTU1dE (this is the login credentials)
 GEOClient: account/123456
 Content-Length: 0

Upvotes: 1

Views: 4146

Answers (2)

Kamafeather
Kamafeather

Reputation: 9845

If you want something more powerful you can use Guzzle that integrates cURL in a really better way.

I don't get exactly what do you mean with passing it via a button. Are you talking just about PHP or also HTML/Javasript? If you want a button you could proceed with a button that performs a XMLHttpRequest through Javascript, calling the API (that should return an application/json Content-Type), and then process this result via Javascript.

Or you can use cURL to call the API, get the result, json_decode it and do what you want with that.

You should explain better what you want to accomplish.

[EDIT]

Ok, after the clarifications I can tell you that you have to divide the parameters for the API from the settings for the HTTP call to the API (as you already did ^^ ).

So the parameters that will make the API to perform something are

  • action = login

while all the others are all setting for the HTTP client (cURL or Guzzle) that will perform the calling to the API's url

  • method = POST
  • Host = api.interlinkexpress.com
  • path = /user/
  • Accept = application/json
  • Content-Type = application/json
  • Authorization = Basic
  • GEOClient = account/123456 (is this a specific header needed from you API?)
  • Content-Length = 0

So, this is not complete nor tested but should give you a path to follow for adding

    $data = array("action" => "login");

    $data_string = json_encode($data);

    $ch = curl_init(sprintf('%s/%s', 'http://api.interlinkexpress.com', 'user/');
    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)),
        'GEOClient: account/123456',
    );
    // I would suggest you to add the timeout
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY); // or set it to CURLAUTH_BASIC
    curl_setopt($ch, CURLOPT_USERPWD, sprintf('%s:%s', $data['username'],$data['password']));
    $status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);   //get status code

    $result = curl_exec($ch);

I honestly don't get/know where you should send JSON encoded data. This way you are sending the 'action' as JSON data, but I guess that this is wrong. Maybe you want to pass the $data_string as the value of a specific field? (like in the example stated from PHP Develoer?)

Work on that, and understand exactly what data you have to pass as JSON and what NOT instead.

I hope to have helped you and gave good suggestions.

Upvotes: 2

Jigisha Variya
Jigisha Variya

Reputation: 207

Your $data_string is not in format like (field=value). Nothing to do just pass array into POST.

curl_setopt($ch, CURLOPT_POSTFIELDS, array('Data'=>$data_string));

Upvotes: 1

Related Questions