Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send json post using php

I have this json data:

{      userID: 'a7664093-502e-4d2b-bf30-25a2b26d6021',     itemKind: 0,     value: 1,     description: 'Saude',     itemID: '03e76d0a-8bab-11e0-8250-000c29b481aa' } 

and I need to post into json url: http://domain/OnLeagueRest/resources/onleague/Account/CreditAccount

using php how can I send this post request?

like image 584
CMartins Avatar asked Jun 02 '11 10:06

CMartins


People also ask

How can get cURL output in JSON format in PHP?

To get JSON with Curl, you need to make an HTTP GET request and provide the Accept: application/json request header. The application/json request header is passed to the server with the curl -H command-line option and tells the server that the client is expecting JSON in response.


2 Answers

You can use CURL for this purpose see the example code:

$url = "your url";     $content = json_encode("your data to be sent");  $curl = curl_init($url); curl_setopt($curl, CURLOPT_HEADER, false); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_HTTPHEADER,         array("Content-type: application/json")); curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_POSTFIELDS, $content);  $json_response = curl_exec($curl);  $status = curl_getinfo($curl, CURLINFO_HTTP_CODE);  if ( $status != 201 ) {     die("Error: call to URL $url failed with status $status, response $json_response, curl_error " . curl_error($curl) . ", curl_errno " . curl_errno($curl)); }   curl_close($curl);  $response = json_decode($json_response, true); 
like image 150
Muhammad Zeeshan Avatar answered Sep 28 '22 14:09

Muhammad Zeeshan


Without using any external dependency or library:

$options = array(   'http' => array(     'method'  => 'POST',     'content' => json_encode( $data ),     'header'=>  "Content-Type: application/json\r\n" .                 "Accept: application/json\r\n"     ) );  $context  = stream_context_create( $options ); $result = file_get_contents( $url, false, $context ); $response = json_decode( $result ); 

$response is an object. Properties can be accessed as usual, e.g. $response->...

where $data is the array contaning your data:

$data = array(   'userID'      => 'a7664093-502e-4d2b-bf30-25a2b26d6021',   'itemKind'    => 0,   'value'       => 1,   'description' => 'Boa saudaÁ„o.',   'itemID'      => '03e76d0a-8bab-11e0-8250-000c29b481aa' ); 

Warning: this won't work if the allow_url_fopen setting is set to Off in the php.ini.

If you're developing for WordPress, consider using the provided APIs: https://developer.wordpress.org/plugins/http-api/

like image 37
David Riccitelli Avatar answered Sep 28 '22 16:09

David Riccitelli