1

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
2
  • What's your concern? safety? You can send/get via cURL, via file_get_contents or fsockopen, depending on what's supported by server. Commented Aug 27, 2014 at 10:21
  • The main concern is safety I know our server supports cURL and file_get_contents Commented Aug 27, 2014 at 10:36

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

8 Comments

Sorry I didn't explain that very well. Basically someone on our end will click a button and it will fire to the php page so it can run. Would I need to pass the information through via the button or can the information be passed through by just being on the php page?
If the information you have to pass are inserted into the calling page where the button is, you need some Javascript to collect them and send them to the API (in the right parameter forma for the API). Or if you have a form you can set the action to call the PHP page, send its fields by method POST or GET. I didn't get if your PHP page is the API or if the API is something you don't have control on. And if you need to call the API with the button, or you have to pass from the PHP page first (and then call the API with cURL/Guzzle)
The PHP page contains the API however it also contains other functions which update certain parts of our system. I believe I can call the API on the PHP page as it will just sit at the top
I suppose the easiest way to explain it is basically how do I pass this information to the API using PHP: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
Usually an API is something that provides an interface for calling it, and you don't need to know about its internal implementation. So, calling the API from the button is one thing. How then the internal API functions are executed into the PHP page is another thing. If I understood well in this case the API IS the PHP page (the host you call [api.interlinkexpress.com/user/], with the parameters for the service you want [action=login]).
|
1

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));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.