How to transform body of post in a querystring - php

I have a request to do in a API and i cant make a request using body in method GET. But, as there no way to make this, the only way that i find to make is transforming a post body in a querystring and put in url.
I read some questions here, and the only way that i find to make this.
If have another way, pls tell me.
This is the body that i need transform in querystring:
{"start":{"from":1609815601000,"to":-1}, "contentToRetrieve":["sdes"]}

I did it this way:
$data = array(
'{"start":{"from":' => '1609815601000',
'"to":' => '-1}',
'"contentToRetrieve":' => '["sdes"]}'
);
#Transformando payloadBody in questystring:
$dataformated = http_build_query($data);
Response: $data
= Array
(
[{"start":{"from":] => 1609815601000
["to":] => -1}
["contentToRetrieve":] => ["sdes"]}
)
Response $dataformate: %7B%22start%22%3A%7B%22from%22%3A=1609815601000&%22to%22%3A=-1%7D&%22contentToRetrieve%22%3A=%5B%22sdes%22%5D%7D

Related

JSON API response from endpoint retrieve value and decode

The endpoint send a
Response
WP_HTTP_Requests_Response Object
(
[response:protected] => Requests_Response Object
(
[body] => {“status”:”success”,”result”:”112″,”code”:200}
I was trying to get the “result:”112”
Is there a way I can retrieve it?
Thank you. 🙂
Refer to the documentation here: https://developer.wordpress.org/reference/classes/wp_http_requests_response/
Assuming the response you are getting (WP_HTTP_Requests_Response) is stored in a variable called $response you should do:
$response_data = json_decode($response->get_response_object()->body);
$result = $response_data->result; // this is the result you want

Guzzle 6 POST request

Im just want to ask if anyone knows how i can post multiple data with the POST request.
This works:
$data = array(
'post_params'=>[
'Name'=>'Foo',
'LastName'=>'Bar'
]
);
This dosent work: any alternatives?
$data = array(
'post_params'=>[
'Name'=>'Foo',
'LastName'=>'Bar'
],
'post_params'=>[
'Name'=>'Foo',
'LastName'=>'Bar'
]
);
Ho can i send multiple post data at once?
You can't do it that way just because that's not how requests work, you can append nearly as many attributes as you want, but not nested arrays of data.
Gonna have to do it this way:
$data = array(
'post_params'=>[
'Name1'=>'Foo',
'LastName1'=>'Bar',
'Name2'=>'Foo',
'LastName2'=>'Bar'
]
);

How to get the parameters after an oauth fetch

I'm trying to get some parameters passed in an OAuth fetch.
In the first script, I'm making the Oauth request this way.
$ids = array( 'a' => 1, 'b' => 2);
$oauth = new OAuth("consumer_key","consumer_secret");
$url = $this->getUrlApi();
$oauth->fetch($url,array('ids' => $ids),OAUTH_HTTP_METHOD_POST);
In the second, I'm trying to get the parametrs i've passed in the query. I'm getting an empty parameter.
$ids = $_REQUEST['ids'];
What the wrong thing in my code please. Thanks

CURL Get Request with a parameter that contains a GET url

I'm trying to make a cURL GET to scrape a Facebook Graph object:
GET https://graph.facebook.com/?id=**OBJECT_URL**&scrape=true&method=post
In my case, OBJECT_URL contains GET parameters:
https://www.example.com/og.php?a=b&c=d
For that reason I can't have it as a GET parameter in file_get_contents() or CURLOPT_URL, as it'd turn out something like this:
https://graph.facebook.com/?id=**https://www.example.com/og.php?a=b&c=d**&scrape=true&method=post
Is there a way to pass it as a GET parameter in a way similar to CURLOPT_POSTFIELDS?
You need to escape your parameters, the http_build_query function will be useful:
$query = http_build_query([
'id' => 'http://foo?a=1&b=2',
'scrape' => true,
'method' => 'post'
]);
$url = "https://graph.facebook.com/?".$query;
var_dump($url);
This will output:
https://graph.facebook.com/?id=http%3A%2F%2Ffoo%3Fa%3D1%26b%3D2&scrape=1&method=post

PHP SOAP Request includes multiple identical tags

OK, so I have this external SOAP based webservice, and PHP SoapClient. Everything is fine with basic requests, but I need to create a parameter set that looks like this:
<DatasetList>
<DatasetID>K0001</DatasetID>
<DatasetID>K0002</DatasetID>
</DatasetList>
For a single nested DatasetID tag I'd do:
$req = array( "DatasetList" => array( "DatasetId" => "K0001" ));
$client->getWebserviceCall($req);
but I need multiple DatasetID tags... I've tried assigning DatasetID as an array, but I just get:
<DatasetList>
<DatasetID>Array</DatasetID>
</DatasetList>
Anyone help?
Did you try the array this way?
$req = array( "DatasetList" => array("DatasetID" => array("K0001", "K0002));
You can do this only by wrote the Part with the identical tags by hand. But, the rest of values can you define in a array:
// Define multiple identical Tags for a part of the Array
$soap_var= new SoapVar('
<DatasetID>1</DatasetID>
<DatasetID>2</DatasetID>
';
// Define the other Values in the normal Way as an array
$req = array(
"DatasetList" => $soap_var,
'value2'=>array('other'=>'values'
);

Categories