Can't get basic remote API value shown in PHP - php

This is my code, where I am trying to show the values from the remote API which I am trying to fetch via a .php file in Wordpress.
<?php
try {
$response = wp_remote_get( 'MYURLHERE', array(
'headers' => array(
'Accept' => 'application/json',
)
) );
if ( ( !is_wp_error($response)) && (200 === wp_remote_retrieve_response_code( $response ) ) ) {
$result = json_decode( wp_remote_retrieve_body( $response, true) );
echo $result['data']['0']['id'];
}
} catch( Exception $ex ) {
//Handle Exception.
}
?>
Getting the following error:
Fatal error: Uncaught Error: Cannot use object of type stdClass as array
What am I doing wrong?
This should be the array:
Array
(
[data] => Array
(
[0] => Array
(
[id] => 124
[name] => MyName
[supertype] => Mso

In PHP manual, you can see the parameters of JSON Function: https://www.php.net/manual/en/function.json-decode.php
This json_decode line of code is wrong, here's the fix:
$result = json_decode( wp_remote_retrieve_body( $response), true );

You must be outputting a different variable than the one you are getting a error for.
This is because you use json_decode() without any parameters.
This means it will be outputted as an object, not an array.
So the example output you show must originate from some other place than the $result you are trying to echo.

Related

How to check for simplexml http request code?

I need to check for the HTTP Response from a XML endpoint in PHP
http_response_code() returns 200ok, but I need to know the response from the actual endpoint.
I think I am blocked on the firewall because of prior failures.
Please be kind and ask me if I am not making myself clear. I understand it should be fairly straightforward.
$auth = base64_encode ( $this->config->item ( "user" ) . ":" . $this->config->item ( "pass" ) );
$context = stream_context_create ( array ('http' => array ('header' => "Authorization: Basic $auth" ) ) );
$unitsXML = file_get_contents ( $this->config->item ( "server_url" ) . "/", false, $context );
libxml_use_internal_errors ( true );
$objXML = simplexml_load_string ( $unitsXML );
var_dump(http_response_code()); // <- this returns 200ok, ut how to get this code but from simplexml http request?
At the moment I only have a blank page with null

cURL error 35: SSL connect error with wp_remote_get() in third party API call not working

I am working on gravity form add-on for our CRM product in wordpress. I have used following code. I don't know why this response is occurring and tried a lot but no getting any hope for it. So, Please look at following output.
CODE
<?php
$request = array(
'headers' => $headers,
'method' => $method,
'sslverify'=>false,
'httpversion'=>'1.0',
'AppAccessKey'=>'MyAccessKey ',
'AppPrivateKey'=>'MyPrivateKey',
'sessiontoken'=>'Mysessiontoken'
);
$response = wp_remote_get($apiUrl, $request );
print_r($response);
?>
But i m getting following OUTPUT in response.
OUTPUT
WP_Error Object
(
[errors] => Array
(
[http_request_failed] => Array
(
[0] => cURL error 35: SSL connect error
)
)
[error_data] => Array
(
)
)

Catch ConnectException from Guzzle

I am trying to retrieve the error message that comes with a GuzzleHttp\Exception\ConnectException in Guzzle 6.
I read that in older version this could be achieved with getResponse().
However, the method now returns null by default.
This is my code:
$responses = Pool::batch($client, $requests($this->list), array(
'concurrency' => 15,
));
foreach ($responses as $event) {
$classname = get_class($event);
$raw = print_r($event,1);
if ($classname == "GuzzleHttp\Exception\ConnectException") {
$response = json_encode((string)$event->getResponse());
}
echo $response;
}
$response is empty.
However, $raw contains these lines:
[0] => GuzzleHttp\Exception\ConnectException Object
(
[request:GuzzleHttp\Exception\RequestException:private] => GuzzleHttp\Psr7\Request Object
(
[method:GuzzleHttp\Psr7\Request:private] => GET
....
)
[response:GuzzleHttp\Exception\RequestException:private] =>
[handlerContext:GuzzleHttp\Exception\RequestException:private] => Array
(
[errno] => 6
[error] => Could not resolve host: mydomain.it
[url] => http://mydomain.it/
[content_type] =>
[http_code] => 0
....
How do I retrieve the "Could not resolve host: mydomain.it" message?
All I found was "you need to catch the error". But how when I am using Pool::batch and everything already is in my responses array?
Use $event->getMessage() (Exception Message) instead of $event->getResponse() because there is no connection, so no response

PHP SOAP Request via SOAPClient to SAP - params missing error

I am doing a PHP SOAP Request as follows (my code, location removed as I'm not allow to publish)
try {
$location = "http://myUrltoWSDLhere";
$client = new SoapClient($location,
array(
'soap_version' => SOAP_1_1,
'login' => 'myuser',
'password' => 'mypass'
));
$params = array(array('BUKRS'=> 1));
$x = $client->ZIbFtiWsCredPosToWeb( $params );
print_r($x);
} catch (Exception $e) {
echo $e->getMessage();
}
I've been trying countless combinations how to pass the params to my method and I am doing a print_r($x) and gives me the below output.
stdClass Object ( [EBkpf] => stdClass Object ( ) [EMsgnr] => 6 [EMsgtxt] => BUKRS is missing [EMsgtyp] => E [ESstMon] => 00000000000000000000 )
Important: SoapClient getTypes returns the following for this method and the IBukrs is the field I am trying to pass data to. In SAP the Company Code is "BUKRS" so I had tried BUKRS and IBukrs and also Bukrs.
Any help would be greatly appreciated!!
[43] => struct ZIbFtiWsCredPosToWeb {
int IAnz;
char1 IAp;
char4 IBukrs;
ZibSstGjahr IGjahr;
ZibSstCredTt IKreditor;
char1 IOp;
numeric20 ISstMon;
}
WSDL Content: http://pastebin.com/fU5PhD9B

Empty POST data in first query

I have a strange problem with PHP script and sending data by POST.
Script source where I receive POST data:
<?php
print_r($GLOBALS);
?>
Script where I send POST data:
<?php
$url = 'http://server.com/script.php';
$data = array(
"request" => '
{
"variable": "some data"
}'
);
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data),
),
);
$context = stream_context_create($options);
echo $result = file_get_contents($url, false, $context);
?>
When I call this script first time it return:
Array ( [GLOBALS] => Array *RECURSION* [_POST] => Array ( ) [_GET] => Array ( ) [_COOKIE] => Array ( ) [_FILES] => Array ( ) )
When I repeat query it's OK - Array show POST data. After a few minutes it is again empty.
I tried set different settings in .htaccess file, but it does not help.
PS. Same script on other server works great.
Do you have any ideas?

Categories