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
Related
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.
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
(
)
)
Like WordPress give us rest API for comments, posts etc. I want to get notification API code.
developer.wordpress.com gives the documentation for how to use notification API.
https://developer.wordpress.com/docs/api/1/get/notifications/$note_ID/.
To use this API, I need WordPress rest API notification code.
How can I get that code.
WordPress rest API code doesn't have notification code.
https://wordpress.org/plugins/rest-api/
Please help me.
You can use following code to get notification id using rest api.
Note : assume id is 1801481297
<?php
$options = array (
'http' =>
array (
'ignore_errors' => true,
'header' =>
array (
0 => 'authorization: Bearer YOUR_API_TOKEN',
),
),
);
$context = stream_context_create( $options );
$response = file_get_contents(
'https://public-api.wordpress.com/rest/v1/notifications/1801481297',
false,
$context
);
$response = json_decode( $response );
?>
I'm trying to parse the mobile.de API.
I have this code to get the XML:
$titan = TitanFramework::getInstance( 'MWPC' );
$handle = curl_init();
$sellerID = $titan->getOption( 'mwpc_seller_id' ).' HTTP/1.0';
$auth_token = base64_encode($titan->getOption( 'mwpc_api_usr' ) . ':' . $titan->getOption( 'mwpc_api_pass' ));
curl_setopt_array(
$handle,
array(
CURLOPT_URL => 'http://services.mobile.de/1.0.0/ad/search?customerId='.$sellerID,
CURLOPT_POST => false,
CURLINFO_CONTENT_TYPE => 'application/xml',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => array(
'Authorization: Basic '. $auth_token,
'accept: application/xml',
'Accept-Language: de, en'
)
)
);
$response = curl_exec($handle);
I get the following XML Response:
<search:result xmlns:resource="http://services.mobile.de/schema/resource" xmlns:seller="http://services.mobile.de/schema/seller" xmlns:ad="http://services.mobile.de/schema/ad" xmlns:search="http://services.mobile.de/schema/search" xmlns:financing="http://services.mobile.de/schema/common/financing-1.0" xmlns:error="http://services.mobile.de/schema/common/error-1.0" total="28" page-size="20" current-page="1" max-pages="2">
<ad:ad key="123456" url="http://services.mobile.de/1.0.0/ad/...">
<ad:creation-date value="2014-12-08T09:51:39+01:00"/>
<ad:modification-date value="2015-01-09T12:56:16+01:00"/>
<ad:detail-page url="http://suchen.mobile.de/auto-inserat/..."/>
<ad:vehicle>
<ad:class key="Car" url="http://services.mobile.de/1.0.0/refdata/classes/Car">
<resource:local-description xml-lang="de">Pkw</resource:local-description>
</ad:class>
<ad:category key="Cabrio" url="http://services.mobile.de/1.0.0/refdata/categories/Cabrio">
<resource:local-description xml-lang="de">Cabrio/Roadster</resource:local-description>
</ad:category>
<ad:make key="MINI" url="http://services.mobile.de/1.0.0/refdata/classes/Car/makes/MINI">
<resource:local-description xml-lang="de">MINI</resource:local-description>
</ad:make>
<ad:model key="COOPER_SD_CABRIO" url="http://services.mobile.de/1.0.0/refdata/classes/Car/makes/MINI/models/COOPER_SD_CABRIO">
<resource:local-description xml-lang="de">Cooper SD Cabrio</resource:local-description>
</ad:model>
And the XML object handling:
$XML_Obj = simplexml_load_string($response);
echo '<pre>';
print_r($XML_Obj);
echo '</pre>';
The above code will output:
SimpleXMLElement Object
(
[#attributes] => Array
(
[total] => 28
[page-size] => 20
[current-page] => 1
[max-pages] => 2
)
)
How can I echo the data from inside the "ad:foo"?
I stuck at this point!! Was googling for many hours :(
edit:
If I use this code from suggestion I get an empty array:
$att = $XML_Obj->xpath("//ad[#key='car']");
You can use SimpleXML's xpath function to search by attributes. e.g.:
$XML_Obj = simplexml_load_string($response);
$att = $XML_Obj->xpath("//ad[#key='foo']");
Struggled two days with the same problem. First of all - never use print_r or var_dump for debug outputs on SimpleXMLElement. Instead use these functions: https://github.com/IMSoP/simplexml_debug.
Second problem is namespaces in xml. Dirty hack is str_replace to remove the XML namespace: https://www.hacksparrow.com/how-to-manhandle-xml-with-namespace-in-php.html
And final solution, that works like a charm:
https://outlandish.com/blog/tutorial/xml-to-json/
The #attributes key will have to be referenced like
$XMLobj->{'#attributes'}
You can iterate over it with a foreach()
I have never delt with proxies but now I have to. The program is a PHP parser that scraps several websites to retrieve need info. I just need to know how PHP programs work via proxies... how to make them work via proxies.
Thanks for any help!
You can use curl with CURLOPT_PROXY Option.
http://www.php.net/manual/en/function.curl-setopt.php
http://php.net/stream_context_create
For example:
<?php
$opts = array ( 'http' => array ( 'proxy' => 'tcp://proxy:8080', 'request_fulluri' => true ) ) ;
$context = stream_context_create ( $opts ) ;
$f = file_get_contents ( 'http://yoururl/', false, $context ) ;