I am trying to POST JSON content to a remote REST endpoint, however the 'content' value appears to be empty on delivery. All other headers etc are being received correctly, and the web service tests successfully with a browser based test client.
Is there a problem with my syntax below where I specify the 'content' field?
$data = array("username" => "duser", "firstname" => "Demo", "surname" => "User", "email" => "[email protected]"); $data_string = json_encode($data); $result = file_get_contents('http://test.com/api/user/create', null, stream_context_create(array( 'http' => array( 'method' => 'POST', 'header' => array('Content-Type: application/json'."\r\n" . 'Authorization: username:key'."\r\n" . 'Content-Length: ' . strlen($data_string) . "\r\n"), 'content' => $data_string) ) )); echo $result;
Send JSON data via POST with PHP cURLInitiate new cURL resource using curl_init(). Setup data in PHP array and encode into a JSON string using json_encode(). Attach JSON data to the POST fields using the CURLOPT_POSTFIELDS option. Set the Content-Type of request to application/json using the CURLOPT_HTTPHEADER option.
Accessing JSON data as a PHP object By default the json_decode() function returns an object. To access the PHP object data, you use the object operator (->) after the object name, followed by the key of the key-value pair. This is the same as the name in the name-value pair in JSON object eg $data->firstName .
The file_get_contents() reads a file into a string. This function is the preferred way to read the contents of a file into a string. It will use memory mapping techniques, if this is supported by the server, to enhance performance.
This is the code I always use and it looks pretty similar (though this is of course for x-www-form-urlencoded). Perhaps your username:key
needs to be base64_encode
'd.
function file_post_contents($url, $data, $username = null, $password = null) { $postdata = http_build_query($data); $opts = array('http' => array( 'method' => 'POST', 'header' => 'Content-type: application/x-www-form-urlencoded', 'content' => $postdata ) ); if($username && $password) { $opts['http']['header'] = ("Authorization: Basic " . base64_encode("$username:$password")); } $context = stream_context_create($opts); return file_get_contents($url, false, $context); }
The question was about json
, why the accepted answer is about x-www-form
?
Json
has many cool stuff to struggle about, like utf8_encode
function my_utf8_encode(array $in): array { foreach ($in as $key => $record) { if (is_array($record)) { $in[$key] = my_utf8_encode($record); } else { $in[$key] = utf8_encode($record); } } return $in; } function file_post_contents(string $url, array $data, string $username = null, string $password = null) { $data = my_utf8_encode($data); $postdata = json_encode($data); if (is_null($postdata)) { throw new \Exception('decoding params'); } $opts = array('http' => array( 'method' => 'POST', 'header' => 'Content-type: application/json', 'content' => $postdata ) ); if (!is_null($username) && !is_null($password)) { $opts['http']['header'] .= "Authorization: Basic " . base64_encode("$username:$password"); } $context = stream_context_create($opts); try { $response = file_get_contents($url, false, $context); } catch (\ErrorException $ex) { throw new \Exception($ex->getMessage(), $ex->getCode(), $ex->getPrevious()); } if ($response === false) { throw new \Exception(); } return $response; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With