96

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?

CMartins
  • 2,952
  • 4
  • 34
  • 52

4 Answers4

145

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);
Muhammad Zeeshan
  • 8,592
  • 10
  • 44
  • 55
  • 1
    I have some more values than JSON -- Yes, I have JSON value too.. How do I do that now? I have altogether three values to be posted: title=somevalue&hash=somevalue&json = JSON VALUE . Now how to do this using php? – LIGHT Sep 25 '12 at 14:53
  • 1
    This did not work in my situation (http://stackoverflow.com/questions/34021817/sending-data-through-curl-in-a-specified-format). My nodejs received {}. Have any idea why? – Fane Dec 02 '15 at 12:09
  • If you benchmark this and the native `file_get_contents` method, they're about exactly the same speed – Brian Leishman Oct 04 '18 at 19:27
  • 1
    [curl example and user notes in php manual](https://www.php.net/manual/en/curl.examples-basic.php) – ToolmakerSteve Apr 10 '19 at 09:15
  • @LIGHT - `$content = http_build_query( array( 'key1' => 'value1', 'key2' => 'value2', 'json' => json_encode($array_to_become_json) ) );` where you've prepped `$array_to_become_json = array(...)` earlier. – ToolmakerSteve Apr 10 '19 at 10:04
  • It's giving brackets syntax error, if we pass json object to json encode function. – Rajat Sep 28 '20 at 21:34
139

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/

Peter VARGA
  • 4,167
  • 3
  • 33
  • 69
David Riccitelli
  • 7,081
  • 4
  • 39
  • 54
  • 1
    I know I'm years late, but is it possible to get at the response headers with this method? – solarshado Mar 31 '16 at 04:39
  • 4
    Awesome, thanks for mentioning doing this without any extensions! – joonas.fi Jun 16 '16 at 19:45
  • Without extensions is always welcome. I didn't know that WP had a standardized API for this. Extra thanks for that bit of info! – Vedran Šego Nov 01 '16 at 11:06
  • 2
    @solarshado raw response headers can be fetched with [stream_get_meta_data](http://php.net/manual/en/function.stream-get-meta-data.php). – Daniels118 Apr 28 '17 at 07:41
  • 2
    A small improvement suggestion, you could also put the headers in an array, especially if more headers such as Authorization need to be added. In that case, the "\r\n" should *not* be added. – zero0cool Aug 09 '17 at 14:55
3

Beware that file_get_contents solution doesn't close the connection as it should when a server returns Connection: close in the HTTP header.

CURL solution, on the other hand, terminates the connection so the PHP script is not blocked by waiting for a response.

OndrejC
  • 124
  • 1
  • 3
0

use CURL luke :) seriously, thats one of the best ways to do it AND you get the response.

Knobik
  • 405
  • 8
  • 26