111

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 to JSON URL: http://domain/OnLeagueRest/resources/onleague/Account/CreditAccount

How can I send this POST request using PHP?

ggorlen
  • 44,755
  • 7
  • 76
  • 106
CMartins
  • 3,247
  • 5
  • 38
  • 53
  • Really, charging an account over _http_! Please use http*s* and a form of JWS to verify who sent it! – Hack5 Jun 24 '17 at 06:54

3 Answers3

159

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,722
  • 10
  • 45
  • 55
152

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 containing 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 file.

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

ggorlen
  • 44,755
  • 7
  • 76
  • 106
David Riccitelli
  • 7,491
  • 5
  • 42
  • 56
  • 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
2

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.

ggorlen
  • 44,755
  • 7
  • 76
  • 106
OndrejC
  • 122
  • 1
  • 3