I have this curl command that works on command line to create wordpress post
curl --user admin:password -X POST http://localhost/wordpress/wp-json/posts --data "title=value1&content_raw=value2"
So I want to convert in into Curl with PHP, this the code that I tried
<?php
$username = "admin";
$password = "password";
$host = 'http://localhost/wordpress/wp-json/posts';
$data = array(
'title'=>'Test WP API',
'content_raw'=>'Hi this is a test WP API'
);
$process = curl_init($host);
curl_setopt($process, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
curl_setopt($process, CURLOPT_HEADER, 1);
curl_setopt($process, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($process, CURLOPT_TIMEOUT, 30);
curl_setopt($process, CURLOPT_POST, 1);
curl_setopt($process, CURLOPT_POSTFIELDS, $data);
curl_setopt($process, CURLOPT_RETURNTRANSFER, TRUE);
$return = curl_exec($process);
However I keep getting error with this code. Below is the CURL result
HTTP/1.1 100 Continue
HTTP/1.1 400 Bad Request
Date: Tue, 10 Mar 2015 06:07:13 GMT
Server: Apache/2.4.9 (Win64) PHP/5.5.12
X-Powered-By: PHP/5.5.12
X-Pingback: http://localhost/jknsapc/xmlrpc.php
Expires: Wed, 11 Jan 1984 05:00:00 GMT
Cache-Control: no-cache, must-revalidate, max-age=0
Pragma: no-cache
X-Content-Type-Options: nosniff
Content-Length: 75
Connection: close
Content-Type: application/json; charset=UTF-8
[{"code":"json_missing_callback_param","message":"Missing parameter data"}]
Can you guys point me which part of the code that I should fix? Update : Problem solved. For guys that encounter the same problem as mine, can refer to my edited question.
Thanks
UPDATE
I found this code to be working, I just need to json_encode the data and CURLOPT_HTTPHEADER is not needed
$username = "admin";
$password = "password";
$host = 'http://localhost/wordpress/wp-json/posts';
$data = array(
'title'=>'Test WP API',
'content_raw'=>'Hi this is a test WP API'
);
//json encode the data to pass via CURL
$data = json_encode($data);
$process = curl_init($host);
curl_setopt($process, CURLOPT_HEADER, 1);
curl_setopt($process, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($process, CURLOPT_TIMEOUT, 30);
curl_setopt($process, CURLOPT_POST, 1);
curl_setopt($process, CURLOPT_POSTFIELDS, $data);
curl_setopt($process, CURLOPT_RETURNTRANSFER, TRUE);
$return = curl_exec($process);