Created
April 19, 2020 13:51
-
-
Save prodhan/824fe74b558d2a385482d7253d6eedab to your computer and use it in GitHub Desktop.
wordpress API :: create post error
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use GuzzleHttp\Client; | |
use GuzzleHttp\Exception\ClientException; | |
function makePost(){ | |
$uri = 'localhost/wordpress-api/wp-json/wp/v2/posts'; | |
$apikey = 'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwOlwvXC9sb2NhbGhvc3RcL3dvcmRwcmVzcy1hcGkiLCJpYXQiOjE1ODcwNzEwMDcsIm5iZiI6MTU4NzA3MTAwNywiZXhwIjoxNTg3Njc1ODA3LCJkYXRhIjp7InVzZXIiOnsiaWQiOiIxIn19fQ.yfBzLQS0oLE4sj45Pm73w6DkS7e2eFDffmQhvw5OyLY'; | |
$myBody['title'] = "Demo Title"; | |
$myBody['content'] = "lorem ipsum"; | |
$myBody['status'] = 'publish'; | |
$client = new Client(); | |
try { | |
$response = $client->request('POST', $uri, ['headers' => ['Accept' => 'application/json', 'Content-type' => 'application/json', 'Authorization' => $apikey], | |
'form_params' => $myBody]); | |
} | |
catch (ClientException $e){ | |
return response()->json(array('response'=> $e->getMessage()), $e->getCode()); | |
} | |
if(200 == $response->getStatusCode()){ | |
$data = json_decode($response->getBody(),false); | |
return response()->json(array('response'=> $data->id), 200); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment