-1

i am trying to send json as raw (html) to wordpress api but its not working for me

$json = json_encode([
'key1' => 'value1',
'key2' => 'value2',
'key3' => 'value3'
]); 

$query = http_build_query([
'title' => $titleFile,
'content' => json_decode(stripslashes($json))
]);
$username = 'admin';
$password = '123456';
$url = "http://mysite/wp-json/wp/v2/posts/?$query"; 
$cmd = "curl --request POST --user $username:$password \"$url\"";
$requestWP = shell_exec($cmd);
print_r($requestWP);  
die();

its working fine, but the post is made with json in the body format if I don't decode and if I decode the post is empty as its an array how can I send the value of the key in raw (html) not Json?

wordpress api endpoint https://developer.wordpress.org/rest-api/reference/posts/

output with json_decode()

stdClass Object ( [key1] => value1 [key2] => value2 [key3] => value3 ) 
Umair
  • 45
  • 1
  • 7

2 Answers2

0

I dont think its the best way to do this but worked for me

$json = json_encode([
'key1' => 'value1',
'key2' => 'value2',
'key3' => 'value3'
]); 

 $data = json_decode($json, TRUE);
 $data = implode(',', $data);
 $data = str_replace(',', '', $data);

$query = http_build_query([
'title' => $titleFile,
'content' => $data
]);

$username = 'admin';
$password = '123456';
$url = "http://mysite/wp-json/wp/v2/posts/?$query"; 
$cmd = "curl --request POST --user $username:$password \"$url\"";
$requestWP = shell_exec($cmd);
print_r($requestWP);  
die();
Umair
  • 45
  • 1
  • 7
0

OK, so there are a few different things in your code that should be addressed. I'm not a Wordpress expert, so I will give you some general guidelines:

Making a POST request

There are a few different ways of sending a HTTP request from PHP, either using native functions or libraries, but using shell_exec to call CURL is definitely not the best. If you want to use CURL, I'd suggest the CURL library. Otherwise try a library like Guzzle

Payload goes in the body

When sending a POST request, the correct way to send the data (and the way Wordpress is expecting it) is to send it in the body of the request, not as part of the query string as you seem to be doing.

Payload should be a JSON string

You should do something similar to the following snippet to generate the body of your request if you are going to use CURL:

$data = json_encode([
    'title' => 'my title',
    'content' => [
        'key1' => 'value1',
        'key2' => 'value2',
        'key3' => 'value3'
    ]
]);

$data will then have the JSON string:

"{"title":"my title","content":{"key1":"value1","key2":"value2","key3":"value3"}}"

Guzzle for instance, will take care of that for you.

Authentication

The endpoint you are trying to hit is protected and using login/password won't work unless you are using the Basic Authentication plugin.

Putting it all together

This is all you should need if you decide to use Guzzle and Basic Authentication:

$client = new GuzzleHttp\Client();

$client->post('http://mysite/wp-json/wp/v2/posts/', [
  'auth' => [$username, $password],
  'json' => $data
]);
lbrandao
  • 4,144
  • 4
  • 35
  • 43