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 )
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();
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
]);
Related
I've tried to create an HTTP request via the Guzzle libary, but it outputs the error
Cannot use object of type stdClass as array
$body = array("foo" => "bar");
$response = self::$client->request("POST", $url, array(
"form_params" => $body,
"auth" => $auth
));
I tried the following and it worked:
$response = self::$client->request("POST", $url, array(
"form_params" => array("foo" => "bar"),
"auth" => $auth
));
Sadly this isn't the solution I'm searching for, because I want to use it in a generic method, where I transfer the form parameters from another class.
P.S. I've found the following post, but it got obvious problems that I don't have in my code:
Guzzle form_params not accepting array
I've tried dozens of times to debug this, but I can't find where my error code. The problem is simple, I just want to post data with the application/json format
$this->client = new GuzzleHttp\Client(['base_uri' => 'https://myendpointapi.com/']);
$headers = [
'Authorization' => $auth,
'Accept'=>'application/json',
'Content-Type' => $contentTypes
];
$url = "my-service";
$data = ["foo" => "bar"];
$this->client->send('POST',$url,["headers" => $headers, "json" => $data ]);
but the error I cant post $data, the response inform me no data send by me. I am also try to change $data value like
$data = array(["foo" => "bar"])
or change the format to multipart request (application/x-www-form-urlencoded) but the response keep same. I don't know why my code is not send the data.
This is the Request body that I need to send- (Curl POST request in PHP)
$data = {
"paramOne" : "f733787d-5649",
"paramTwo": {
"format": "123XD"
},
"paramThree": [
{"type":"cn", "value":"Test User Manish 1"},
{"type":"c", "value":"US"}
]
};
I'm trying to use it at this line of my Curl Request in PHP-
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
The nested params are messing up the format in which I'm trying to send.
I already tried using http_build_query but then paramThree is causing problems.
I'm looking for the changes that I need to make to the format of $data before I use http_build_query on it.
It's JSON. You can either post it as a string (enclosed in quotes) or make an array first, convert it to JSON and then post it. Like this:
$array = [
'paramOne' => 'f733787d-5649',
'paramTwo' => [
'format' => '123XD'
],
'paramThree' => [[
'type' => 'cn',
'value' => 'Test User Manish 1'
],
[
'type' => 'c',
'value' => 'US'
]]
];
$data = json_encode($array);
In both cases use
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
I need to send data to another webpage of different application and it will send some json data which i need to use in further instruction.
I need to send some basic information like cus_name, cus_email, cus_phone to that webpage and that will send some data as json format.
i got the basic idea of how can i catch the json response : like that,
$client = new Client();
$body = $client->get('https://securepay.google.com/gwprocess/v3/api.php')->getBody();
$data = json_decode($body);
return redirect($data->GatewayPageURL);
How do i send those variables to and catch the response in same controller?
Thanks in advance.
You can send Query String Parameters in two ways.
Include them in the URI itself.
$response = $client->request('GET', 'https://securepay.google.com/gwprocess/v3/api.php?cus_name=name&cus_email=email&cus_phone=phone');
Or
Specify them using the query request option as an array
$response = $client->request('GET', 'https://securepay.google.com/gwprocess/v3/api.php', [
'query' => [
'cus_name' => 'name',
'cus_email' => 'email',
'cus_phone' => 'phone'
]
]);
Or for Post Requests:
$response = $client->request('POST', 'https://securepay.google.com/gwprocess/v3/api.php', [
'form_params' => [
'cus_name' => 'name',
'cus_email' => 'email',
'cus_phone' => 'phone'
]
]);
and then to get the response:
$result = json_decode($response->getBody());
I have a service that expects 2 objects... Authentication and Client.
Both are mapped correctly.
I am trying to consume them as Json, but I'm having a hard time doing that.
If I specify just one parameter it works fine, but how can I call this service passing 2 parameters? Always give me some exception.
Here is my rest service:
#POST
#Path("login")
#Consumes("application/json")
public void login(Authentication auth, Client c)
{
// doing something
}
And here is my PHP consumer:
$post[] = $authentication->toJson();
$post[] = $client->toJson();
$resp = curl_post("http://localhost:8080/login", array(),
array(CURLOPT_HTTPHEADER => array('Content-Type: application/json'),
CURLOPT_POSTFIELDS => $post));
I tried some variations on what to put on CURLOPT_POSTFIELDS too but couldn't get it to work.
The problem you are probably having, is that you are declaring $post as a numbered array, which probably contains the array keys you are mapping. Basically, this is what you are giving it:
Array(
1 => Array(
'authentication' => 'some data here'
),
2 => Array(
'client' => 'some more data here'
)
)
When in reality, you should be creating the $post var like so:
Array(
'authentication' => 'some data here',
'client' => 'some more data here'
)
Try changing your code to something more like this (not optimal, but should get the job done):
$authentication = $authentication->toJson();
$client = $client->toJson();
$post = array_merge($authentication, $client);
$resp = curl_post("http://localhost:8080/login", array(),
array(CURLOPT_HTTPHEADER => array('Content-Type: application/json'),
CURLOPT_POSTFIELDS => $post));