-5

I like to post a JSON object with curl. All I have is that piece of code:

curl -X POST \
-H "Accept: application/json" \
-H "X-Access-Token: ###secureToken###" \
-H "Cache-Control: no-cache" \
-d '{
    "frames": [
        {
            "index": 0,
            "text": "SUCCESS",
            "icon": null
        }
    ]
}' \
https://developer.lametric.com/api/V1/dev/widget/update/com.lametric.###appid###

What to do now exactly, to make this happen in PHP? Could you please post an example?

Jotto
  • 3
  • 6

2 Answers2

0
// init curl

$handle = curl_init();

// set options/parameters

curl_setopt( $handle, CURLOPT_URL,              'https://developer.lametric.c...');
curl_setopt( $handle, CURLOPT_CUSTOMREQUEST,    "POST");
curl_setopt( $handle, CURLOPT_POSTFIELDS,       'the-json-encoded-data-here' );
curl_setopt( $handle, CURLOPT_RETURNTRANSFER,   true ); // you want to get the response

// set headers 

curl_setopt( $handle, CURLOPT_HTTPHEADER,       array(  'Accept: application/json',
                                                        '....' ) );
// execute the request and get the response

$response = curl_exec( $handle );

// get the status too

$status = curl_getinfo( $handle, CURLINFO_HTTP_CODE );

// release resources

curl_close( $handle );

Just an example/introduction.

You initialize php's curl.

Setup all the parameters.

Send the request.

I won't write all the code for you.

PHP Reference is clear (and have examples too)

http://php.net/manual/en/book.curl.php

SO have examples too:

PHP + curl, HTTP POST sample code?

Community
  • 1
  • 1
Paolo
  • 13,439
  • 26
  • 59
  • 82
0

Or without curl, very generic pattern that I use to keep dependencies down.

<?php 

$reqBody = array(
    'frames' => array(
        'index' => 0,
        'text' => "SUCCESS",
        'icon' => null
    )
);


$bodyString = json_encode($reqBody);

$access_token = "###secureToken###";
$context_options = array (
    'http' => array (
        'method' => 'POST',
        'header' => "Accept: application/json\r\nX-Access-Token: " . $access_token . "\r\nCache-Control: no-cache\r\nContent-Length: " . strlen($bodyString) . "\r\n",
        'content' => $bodyString
        )
    );
$context_for_post = stream_context_create($context_options);

$response = file_get_contents($"https://developer.lametric.com/api/V1/dev/widget/update/com.lametric.###appid###", FALSE, $context_for_post);

// Check for errors
if(!$response){
    die("<h2>ERROR</h2>");
}

// Decode the response
$responseData = json_decode($response, TRUE);

// some examples of parsing response json ...
if ($responseData['message'] != null) {

}
$this->sessionToken = $responseData['message']['data']['results']['token'];
if($this->sessionToken === FALSE) {
    die('Failed to Parse Response');
}

?>

If the web server doesn't seem to like your post, it might be expecting form-data type of POST, so set up the body and headers like this:

$bodyString = "------WebKitFormBoundaryiAsuvpNuslAE3Kqx\r\nContent-Disposition: form-data; name=\"json\"\r\n\r\n" . 
    json_encode($reqBody) . 
    "\r\n------WebKitFormBoundaryiAsuvpNuslAE3Kqx--\r\n";

$access_token = "###secureToken###";
$context_options = array (
    'http' => array (
        'method' => 'POST',
        'header' => "X-Access-Token: " . $access_token . "\r\nCache-Control: no-cache\r\nAccept: application/json\r\nContent-Type: multipart/form-data; boundary=----WebKitFormBoundaryiAsuvpNuslAE3Kqx\r\n" . "Content-Length: " . strlen($bodyString) . "\r\n",
        'content' => $bodyString
        )
    );
Buddy Yaussy
  • 445
  • 2
  • 7