Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

POST request with JSON body

Tags:

json

post

php

I would like to add a post to a Blogger blog via PHP. Google provided the example below. How to use that with PHP?

You can add a post for a blog by sending a POST request to the post collection URI with a post JSON body:

POST https://www.googleapis.com/blogger/v3/blogs/8070105920543249955/posts/ Authorization: /* OAuth 2.0 token here */ Content-Type: application/json  {   "kind": "blogger#post",   "blog": {     "id": "8070105920543249955"   },   "title": "A new post",   "content": "With <b>exciting</b> content..." } 
like image 972
Matt Avatar asked Jun 04 '13 14:06

Matt


People also ask

How do I send a body in a POST request?

The body format is defined by the Content-Type header. When using a HTML FORM element with method="POST" , this is usually application/x-www-form-urlencoded . Another very common type is multipart/form-data if you use file uploads.

CAN POST request contain body?

A POST request requires a body in which you define the data of the entity to be created. A successful POST request would be a 200 response code. In our weather app, we could use a POST method to add weather data about a new city.

CAN GET request have a JSON body?

Yes. In other words, any HTTP request message is allowed to contain a message body, and thus must parse messages with that in mind.

What is JSON Request body?

Essentially, the Content-Type header specifies a MIME type telling the server what kind of data is in the request body. For example, using the application/json MIME type tells the server that the request body is a valid JSON object.


1 Answers

You need to use the cURL library to send this request.

<?php // Your ID and token $blogID = '8070105920543249955'; $authToken = 'OAuth 2.0 token here';  // The data to send to the API $postData = array(     'kind' => 'blogger#post',     'blog' => array('id' => $blogID),     'title' => 'A new post',     'content' => 'With <b>exciting</b> content...' );  // Setup cURL $ch = curl_init('https://www.googleapis.com/blogger/v3/blogs/'.$blogID.'/posts/'); curl_setopt_array($ch, array(     CURLOPT_POST => TRUE,     CURLOPT_RETURNTRANSFER => TRUE,     CURLOPT_HTTPHEADER => array(         'Authorization: '.$authToken,         'Content-Type: application/json'     ),     CURLOPT_POSTFIELDS => json_encode($postData) ));  // Send the request $response = curl_exec($ch);  // Check for errors if($response === FALSE){     die(curl_error($ch)); }  // Decode the response $responseData = json_decode($response, TRUE);  // Close the cURL handler curl_close($ch);  // Print the date from the response echo $responseData['published']; 

If, for some reason, you can't/don't want to use cURL, you can do this:

<?php // Your ID and token $blogID = '8070105920543249955'; $authToken = 'OAuth 2.0 token here';  // The data to send to the API $postData = array(     'kind' => 'blogger#post',     'blog' => array('id' => $blogID),     'title' => 'A new post',     'content' => 'With <b>exciting</b> content...' );  // Create the context for the request $context = stream_context_create(array(     'http' => array(         // http://www.php.net/manual/en/context.http.php         'method' => 'POST',         'header' => "Authorization: {$authToken}\r\n".             "Content-Type: application/json\r\n",         'content' => json_encode($postData)     ) ));  // Send the request $response = file_get_contents('https://www.googleapis.com/blogger/v3/blogs/'.$blogID.'/posts/', FALSE, $context);  // Check for errors if($response === FALSE){     die('Error'); }  // Decode the response $responseData = json_decode($response, TRUE);  // Print the date from the response echo $responseData['published']; 
like image 148
Rocket Hazmat Avatar answered Sep 18 '22 23:09

Rocket Hazmat