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
Related
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
I need to display database table content in php using web service. in there i used WSDL service with php. this is i tried code,
Test.php
<?php
/* Initialize webservice with your WSDL */
$client = new SoapClient("http://localhost:23995/Service1.svc?wsdl");
/* Invoke webservice method with your parameters, in this case: Function1 */
$response = $client->__soapCall("SelectUserDetails", array());
/* Print webservice response */
print_r($response);
?>
Output
> stdClass Object ( [SelectUserDetailsResult] => stdClass Object (
> [schema] => [any] => 1rooter12345rooter#gmail.com
> 2kal123ukkal#gmail.com 3sam123uksam#gmail.com 4net1234uknet#gmail.com
> ) )
How can i display these database values in HTML table with php
As per your output the response is an Object.If you are not familiar with PHP objects read this, As an example i can provide you the following code to access the Objects.
In PHP you can access and object by the following way :
$myObject = new stdClass(); //creating an object for demo
$myObject->name = 'My Name';
$myObject->address = 'My address1';
echo $myObject->name; // Output: My Name
echo $myObject->address; // Output: My address1
In the response, variable $response have the following members :
SelectUserDetailsResult // An Object with two members ('schema' & 'any'). In which schema is null, but you have some data in the member 'any'.
You can access the member 'any' as shown below :
$allEmails = $response->SelectUserDetailsResult->any;
By this way you can use this variable to print its value in any html.
I am working on laravel app and I am saving my data in JSON encoded form as
{"name":"Ali","email":"testdc#gamil.com"}
It shows as above in db text field
In my method I am getting data as
function users($id, Request $request)
{
$method = $request->method();
if($request->isMethod('GET')) {
$users = DB::table('user_settings')->select('notification_setting')->first();
print_r( $notification_smtp);
die;
return view('setting/user');
}
}
Below is the output of the code above:
stdClass Object ( [notification_setting] => {"name":"Ali","email":"testdc#gamil.com"} )
If I try to decode it it gives an error as json_decode 2nd parameter should be string object passed
How can I get the response to this format?
stdClass Object ( [name] => Ali [email] => test#gmail.com )
How can I possibly achieve that?
Try, print_r( json_decode($notification_smtp->notification_setting) );
Did you try serialization? like get a tmp variable with parsed object
//your code
$users = DB::table('user_settings')->select('notification_setting')->first();
print_r( $notification_smtp);
die;
//try this
$tmp = (string) $user;
dd($tmp);
if not help, read laravel doc, its really good.
Laravel Serialization
If still no helping you, google serialization and unserialization ;)
I don't know how to work with such object, i need to get first and second one status value, tryed to convert it to json, but it gives me nothing. I just don't get it how to open array with such "_data:MailWizzApi_Params:private" name.
Source:
// SEARCH BY EMAIL
$response = $endpoint->emailSearch($myConfig["LIST-UNIQUE-ID"], $_GET["email"]);
// DISPLAY RESPONSE
echo '<hr /><pre>';
print_r($response->body);
echo '</pre>';
I receive such answer
MailWizzApi_Params Object
(
[_data:MailWizzApi_Params:private] => Array
(
[status] => success
[data] => Array
(
[subscriber_uid] => an837jdexga45
[status] => unsubscribed
)
)
[_readOnly:MailWizzApi_Params:private] =>
)
In this case, you can't.
Because it's private field.
For public fields with "incorrect" names you can use snippet:
$name = '}|{';
$obj->$name;
So, let see to your property: [_data:MailWizzApi_Params:private].
It is private field of instance of MailWizzApi_Params class with _data name.
Let's google to it's implementation: Found
As you can see it has toArray public method. Just use it.
print_r($response->body->toArray());
It has ArrayAccess implemented also. So, $response->body['status'] or $response->body['data'] will works.
Thank you guys for fast answers, here is my dumb way if reading status value
(thanks, #Jose Manuel Abarca Rodríguez)
$toJson = json_encode((array)$response->body);
$toJson = str_replace(array("\u0000MailWizzApi_Params\u0000_"), "", $toJson);
So we receive a normal json:
{"data":{"status":"success","data":{"subscriber_uid":"an837jdexga45","status":"unsubscribed"}},"readOnly":false}
And now we need just to decode it
$json = json_decode($toJson, true);
echo $json['data']['status'];
I am trying to consume a SOAP based webservice uisng PHP. Following is the sample code. I need to know/learn how to access the retur'ed object elements?
Please note i am new to PHP
$url = 'http://www.webservicex.net/uszip.asmx?WSDL';
$soap = new SoapClient($url, array(
"trace" => 1, // enable trace to view what is happening
"exceptions" => 0, // disable exceptions
"cache_wsdl" => 0) );
try {
$result = $soap->GetInfoByZIP(array('USZip' => '97219'));
echo($result->$CITY);
//print_r( $soap->GetInfoByZIP(array("USZip" => "97219")));
} catch (SoapFault $e) {
echo "Error: {$e->faultstring}";
}
I get the following exception
Notice: Undefined variable: CITY
Fatal error: Cannot access empty property
However when I execute the commented line above it return the following response
stdClass Object
(
[GetInfoByZIPResult] => stdClass Object
(
[any] => <NewDataSet xmlns=""><Table><CITY>Portland</CITY><STATE>OR</STATE><ZIP>97219</ZIP><AREA_CODE>503</AREA_CODE><TIME_ZONE>P</TIME_ZONE></Table></NewDataSet>
)
)
So this means that data is being returned but i am not able to access it like the way done in .NET
Can anyone please help me how to access this object in PHP and why?
First of all, u're using $CITY variable to access $result property and you haven't defined it yet.
So if you want to get "CITY" property inside "result" object you should do it by "$result->City".
According to result you get - it's a xml string, not object. If you want to access string do it this way:
$result->GetInfoByZIPResult->any
You can load string with DomDocument or simplexml lib.