Getting an XML file in a .asp URL - php
I need to grab XML data from an .asp URL and cannot figure out where it goes wrong. Tried:
<?php
$url = "http://bookings.emperordivers.com/webScheduleSpecificXML_all.asp";
$feed = file_get_contents($url);
$xml = simplexml_load_string($feed);
// Display the first post title
echo $xml->Schedules->Schedule[0]->Boat;
I hoped this would work, but suspected that the .asp URL is blocking somehow so alternatively tried:
function get_data($url) {
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$returned_content = get_data('http://bookings.emperordivers.com/webScheduleSpecificXML_all.asp');
$xml = simplexml_load_string($returned_content);
// Display the first post title
echo $xml->Schedules->Schedule[0]->Boat;
In both case a beautiful white screen. Is there a simple trick to get this working?
Your not accessing it correctly:
echo $xml->Schedule[0]->Boat;
The format is:
SimpleXMLElement Object
(
[Schedule] => Array
(
[0] => SimpleXMLElement Object
(
[Start] => 2017-09-24
[Duration] => 7
[Boat] => MV Emperor Orion
[Itinerary] => Best of Maldives
[Dep-Arr] => Male
[Spaces] => 4
[Rates] => SimpleXMLElement Object
(
[Rate] => Array
(
[0] => 1149
[1] => 1,057.08
[2] => 1,367.31
)
)
)
...
Related
Convert Raw JSON Text from getting the URL and make it an IMAGE
I am trying to connect to the external site and get the products there and copy them to another site by using its consumer and secret keys. I have the a URL Link that links from an External Site1. Now I get all the JSON Data (including sales, images, data etc.) by using PHP CURL Here is the code: function get_wc_api_client() { $curl = curl_init(); $site = 'https://samplelink.com/wp-json/wc/v2/products?consumer_key=sample-consumer_key&consumer_secret=sample_consumer_secret'; curl_setopt($curl, CURLOPT_URL, $site ); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 60); $content = curl_exec($curl); echo "<pre>"; print_r( json_decode($content, true)) ; echo "</pre>"; } Any recommendations how to just get the IMAGES and the TITLE of the PRODUCT of the external site, plus the links of each products and display them as a full images and text not JSON text ? Thanks in advance UPDATES: Here is the example code of the Result for the print_r Array ( [0] => Array ( [id] => 4591 [name] => Magic Forest [slug] => magic-forest [permalink] => https://samplesite.com/product/magic-forest/ [date_created] => 2019-03-13T19:11:09 [date_created_gmt] => 2019-03-13T19:11:09 [date_modified] => 2019-03-13T19:12:20 [date_modified_gmt] => 2019-03-13T19:12:20 [type] => simple [status] => publish [featured] => [catalog_visibility] => visible [description] => but as I said, I only need the IMAGE, TITLE of the Products and their links and VIEW them as real IMAGE and TITLE and Link. [images] => Array ( [0] => Array ( [id] => 4932 [date_created] => 2019-03-13T18:54:54 [date_created_gmt] => 2019-03-13T18:54:54 [date_modified] => 2019-03-13T18:55:14 [date_modified_gmt] => 2019-03-13T18:55:14 [src] => https://samplesite.com/wpcom-129014632/wp-content/uploads/2019/03/Pond-image.jpg [name] => Pond Cover [alt] => [position] => 0 ) FOR IMAGE: I needed the image src link to be a Real image when executed, also the title of the product. Is it possible?
Use below code function get_wc_api_client() { $curl = curl_init(); $site = 'https://samplelink.com/wp-json/wc/v2/products?consumer_key=sample-consumer_key&consumer_secret=sample_consumer_secret'; curl_setopt($curl, CURLOPT_URL, $site ); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 60); $content = curl_exec($curl); return json_decode($content, true); } Call this function in your view page $apiData = get_wc_api_client(); foreach($apiData['images'] as $data){ echo '<image src="'.$data['src'].'" title="'.$data['name'].'" alt="'.$data['alt'].'">'; }
getting values from stdClass Object
I try to get values of stdClass Object into php variables. And i looked for other solutions but i couldn't success. Here is my result. stdClass Object ( [messages] => Array ( [0] => stdClass Object ( [apiMessageId] => db55468cdb274551b9e18c973ec780f4 [accepted] => 1 [to] => 1111111 [error] => ) ) [error] => ) I want to get only "apiMessageId" section but as i said i couldn't get this. I tried "$object->messages[0]->apiMessageId;" but no results on the screen. Anyone to help me please? and this is my php code for request. function Conn($url){ $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]); $cikti = curl_exec($curl); curl_close($curl); return str_replace(array("\n","\t","\r"), null, $cikti); } $url1 = "https://platform.clickatell.com/messages/http/send?apiKey=xxx&to=xxx&content=Test+message+text"; $Conn = Conn($url1); $res = json_decode($Conn); echo "<pre>"; print_r($res); echo "</pre>";
I found the solution. This is the query what i need. echo $res->messages[0]->apiMessageId; I wrote before echo $object->messages[0]->apiMessageId; but my variable name is not object. The variable name is $res.
Send XML data to paylease gateway using php curl
I am sending xml data to paylease payment gateway and it shows given below error : Array ( [Credentials] => Array ( [GatewayId] => sssssssssssss [Username] => ssssssssssssss ) [Mode] => Array ( ) [Errors] => Array ( [Error] => Array ( [0] => Array ( [Code] => 146 [Status] => Error [Message] => GatewayId not found. ) [1] => Array ( [Code] => 155 [Status] => Error [Message] => User failed to be authenticated. GatewayId and username don't match. ) [2] => Array ( [Code] => 156 [Status] => Error [Message] => User failed to be authenticated. Username and password don't match. ) ) ) ) I am using curl for sending xml request to payment gateway. Please help me out for sending request to payment gateway . I am using given below code : - $xml = "<?xml version='1.0' encoding='UTF-8'?> <PayLeaseGatewayRequest> <Credentials> <GatewayId>####</GatewayId> <Username>#####</Username> <Password>####</Password> </Credentials> <Mode>Test</Mode> <Transactions> <Transaction> <TransactionAction>CCPayment</TransactionAction> <PaymentReferenceId>CC12345</PaymentReferenceId> <PaymentTraceId>1</PaymentTraceId> <PayerReferenceId>395</PayerReferenceId> <PayeeId>129</PayeeId> <PayerFirstName>John</PayerFirstName> <PayerLastName>Smith</PayerLastName> <CreditCardType>Visa</CreditCardType> <CreditCardNumber>4444444444444448</CreditCardNumber> <CreditCardExpMonth>05</CreditCardExpMonth> <CreditCardExpYear>19</CreditCardExpYear> <CreditCardCvv2>123</CreditCardCvv2> <BillingFirstName>John</BillingFirstName> <BillingLastName>Smith</BillingLastName> <BillingStreetAddress>123 PayLease Street</BillingStreetAddress> <BillingCity>San Diego</BillingCity> <BillingState>CA</BillingState> <BillingCountry>US</BillingCountry> <BillingZip>92123</BillingZip> <TotalAmount>1000.00</TotalAmount> <FeeAmount></FeeAmount> <Message></Message > <IncurFee>No</IncurFee> <SaveAccount>Yes</SaveAccount> </Transaction> </Transactions> </PayLeaseGatewayRequest>"; $url = "https://www.paylease.com/gapi/request.php"; //setting the curl parameters. $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-type: application/xml')); // Following line is compulsary to add as it is: curl_setopt($ch, CURLOPT_POSTFIELDS,"xmlRequest=" . $xml ); curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 500); $data = curl_exec($ch); curl_close($ch); //convert the XML result into array $array_data = json_decode(json_encode(simplexml_load_string($data)), true); echo "<pre>"; print_r($array_data);
json_decode: can't get foreach to work
I'm losing my sight on this.. I can't find a solution and I keep ranting :P I'm using the Movie Database to get a JSON with some info about a movie. Here's my code: <?php $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "https://api.themoviedb.org/3/movie/tt1327773/images?api_key=MY_API_KEY"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($ch, CURLOPT_HEADER, FALSE); curl_setopt($ch, CURLOPT_HTTPHEADER, array( "Accept: application/json" )); $response = curl_exec($ch); curl_close($ch); $posters = json_decode($response, true); foreach ($posters as $poster) { $locandina = $poster[0]->posters->file_path; echo $locandina; } ?> Of course print_r($response); works and it prints the array. What doesn't work is the foreach.. I can't get the file_path values. I keep getting Trying to get property of non-object even though I added a , true in the json_decode arguments! Here's the array: [id] => 132363 [backdrops] => Array ( [0] => stdClass Object ( [aspect_ratio] => 1.78 [file_path] => /zZggWJSG18wPIOrZOgV5LI12LMi.jpg [height] => 1080 [iso_639_1] => [vote_average] => 5.3846153846154 [vote_count] => 2 [width] => 1920 ) [1] => stdClass Object ( [aspect_ratio] => 1.78 [file_path] => /wYuFRYTfx5rVjyhqdf8MXdQzKAo.jpg [height] => 1080 [iso_639_1] => [vote_average] => 5.3479853479854 [vote_count] => 2 [width] => 1920 ) ... )
to have access to file_path you need this array access key: $array["backdrops"][0]["file_path"] -> because it is contained in the first position of an non-associative array as the value of "backdrops"... check it out (; source code: // this is your input data $json = '{"id":132363,"backdrops":[{"aspect_ratio":1.78,"file_path":"/zZggWJSG18wPIOrZOgV5LI12LMi.jpg","height":1080,"iso_639_1":null,"vote_average":5.38461538461539,"vote_count":2,"width":1920},{"aspect_ratio":1.78,"file_path":"/wYuFRYTfx5rVjyhqdf8MXdQzKAo.jpg","height":1080,"iso_639_1":null,"vote_average":5.34798534798535,"vote_count":2,"width":1920},{"aspect_ratio":1.78,"file_path":"/txiDfzC43eynxEchHfuZ1xTGfET.jpg","height":1080,"iso_639_1":null,"vote_average":5.34065934065934,"vote_count":2,"width":1920},{"aspect_ratio":1.78,"file_path":"/56jBImSOTXDk77tJ15eDscmM1BO.jpg","height":720,"iso_639_1":null,"vote_average":5.33910533910534,"vote_count":3,"width":1280},{"aspect_ratio":1.78,"file_path":"/b2Ki6hHtAXPIx84mGWM5hkaUXYC.jpg","height":720,"iso_639_1":null,"vote_average":5.2967032967033,"vote_count":2,"width":1280},{"aspect_ratio":1.78,"file_path":"/v765i6Y5oHXKRtQmiTasbs59G6g.jpg","height":720,"iso_639_1":null,"vote_average":5.2967032967033,"vote_count":2,"width":1280},{"aspect_ratio":1.78,"file_path":"/9utSv4T39V70iM1tGPb16HItFZ2.jpg","height":1080,"iso_639_1":null,"vote_average":5.2967032967033,"vote_count":2,"width":1920},{"aspect_ratio":1.78,"file_path":"/fnjBO2DKXOwHVMz4Tpjlf0DAm2F.jpg","height":1080,"iso_639_1":null,"vote_average":5.27472527472527,"vote_count":2,"width":1920},{"aspect_ratio":1.78,"file_path":"/4Ns5u0zZvOvD6FmXCLMsokBF8tO.jpg","height":2160,"iso_639_1":null,"vote_average":5.26041666666667,"vote_count":1,"width":3840},{"aspect_ratio":1.78,"file_path":"/5ioO0UWgnSFO1ESTlci4tvtfO0w.jpg","height":2160,"iso_639_1":null,"vote_average":5.24553571428571,"vote_count":1,"width":3840},{"aspect_ratio":1.78,"file_path":"/6pEA2GiB4rxLLQqoqLKTmJ2bkmk.jpg","height":1080,"iso_639_1":null,"vote_average":5.24542124542125,"vote_count":2,"width":1920},{"aspect_ratio":1.78,"file_path":"/2OJ8l1qsk7KvVO4xdtQ7kDsoInE.jpg","height":2160,"iso_639_1":null,"vote_average":5.23809523809524,"vote_count":1,"width":3840},{"aspect_ratio":1.78,"file_path":"/fFsZzecwN2yTBOPqyQnpQS3lrEh.jpg","height":720,"iso_639_1":null,"vote_average":5.23088023088023,"vote_count":3,"width":1280}],"posters":[{"aspect_ratio":0.67,"file_path":"/a2dAFXwnrRu1kJ95Tp7fI0axyYv.jpg","height":2100,"id":"52d6fdf619c2952d2c048af0","iso_639_1":"de","vote_average":5.41847041847042,"vote_count":3,"width":1400},{"aspect_ratio":0.67,"file_path":"/hUjEYTN5NuK8kYRQxngS7itpBQC.jpg","height":2100,"id":"52b9a79e760ee302e50e0957","iso_639_1":"en","vote_average":5.38992408557626,"vote_count":6,"width":1400},{"aspect_ratio":0.71,"file_path":"/3WuXELbV6MKqqDuOSVcJFFCqiTn.jpg","height":2161,"id":"531fb9259251411f8500067e","iso_639_1":"cs","vote_average":5.3125,"vote_count":1,"width":1529},{"aspect_ratio":0.7,"file_path":"/gkq46U0Cd8zZVfXHsrpCe81xsSH.jpg","height":1500,"id":"53b1277d0e0a26598900797d","iso_639_1":"it","vote_average":5.3125,"vote_count":1,"width":1050},{"aspect_ratio":0.67,"file_path":"/jcyTM8XGonYaKdnvXDZ1hFyzQt6.jpg","height":1024,"id":"51c061f4760ee306480c6370","iso_639_1":"en","vote_average":5.28138528138528,"vote_count":3,"width":690},{"aspect_ratio":0.67,"file_path":"/qx7VAo0aJwH6t17YgGvHe9eP61m.jpg","height":2100,"id":"52be5e01760ee359630bcba1","iso_639_1":"pt","vote_average":5.27529761904762,"vote_count":1,"width":1400},{"aspect_ratio":0.75,"file_path":"/43qeAVpny8rVFW0hprgIFOByO3E.jpg","height":2060,"id":"5231f2ed19c2950c0d03d534","iso_639_1":"fr","vote_average":5.27417027417027,"vote_count":3,"width":1545},{"aspect_ratio":0.7,"file_path":"/b91OJaB9llSUZXc7C8Rtr56e3ws.jpg","height":1139,"id":"530da421c3a3685bf4002347","iso_639_1":"da","vote_average":5.26785714285714,"vote_count":1,"width":800},{"aspect_ratio":0.67,"file_path":"/gBLjprwQ4Re83qF3s5wR17pmQuI.jpg","height":1202,"id":"520b7e0119c2955c22062095","iso_639_1":"en","vote_average":5.26418786692759,"vote_count":10,"width":811},{"aspect_ratio":0.7,"file_path":"/jIFNhNpoZQja02uVudJr4dRzat9.jpg","height":2806,"id":"52b54ad9760ee34c2d0ced9a","iso_639_1":"ro","vote_average":5.26041666666667,"vote_count":1,"width":1956},{"aspect_ratio":0.75,"file_path":"/fR5D7pvKlvdkYf5pjg5v62QA9VY.jpg","height":3778,"id":"52315424760ee370e8179f24","iso_639_1":"fr","vote_average":5.24542124542125,"vote_count":2,"width":2833},{"aspect_ratio":0.71,"file_path":"/zU0FQydIMqusfC7dRD1gJojG07v.jpg","height":1980,"id":"52f23c4ec3a3687dfe0838ed","iso_639_1":"de","vote_average":5.24542124542125,"vote_count":2,"width":1400},{"aspect_ratio":0.67,"file_path":"/ahK6dhq7yVx1O6P8rVnSApurwOR.jpg","height":1200,"id":"52daccf3760ee36893077851","iso_639_1":"it","vote_average":5.17113095238095,"vote_count":1,"width":800},{"aspect_ratio":0.67,"file_path":"/xPwpuwNQVDh7PCQoyct0Fh4kdYn.jpg","height":2100,"id":"52555c11760ee31fbe0727d5","iso_639_1":"en","vote_average":5.14157014157014,"vote_count":11,"width":1400},{"aspect_ratio":0.67,"file_path":"/wLJpmGFzfn5YDCn3ZTO7WgZRpr4.jpg","height":1897,"id":"51ad01df19c295440007504c","iso_639_1":"en","vote_average":5.13227513227513,"vote_count":9,"width":1280},{"aspect_ratio":0.67,"file_path":"/nEx0vsj0WbXuuej0YJYXZaLtIwU.jpg","height":2100,"id":"52b9a79f760ee319b820b08b","iso_639_1":"en","vote_average":5.11204481792717,"vote_count":5,"width":1400},{"aspect_ratio":0.67,"file_path":"/5Iia0rSQXLPSz4Dnksn6OTgTFGX.jpg","height":1500,"id":"535b80d2c3a36830b00010f5","iso_639_1":"hu","vote_average":0.0,"vote_count":0,"width":1000},{"aspect_ratio":0.69,"file_path":"/mMlG2cFr3za21lKkbu2XpitwtGT.jpg","height":800,"id":"52b4058d760ee3576d02b06c","iso_639_1":"el","vote_average":0.0,"vote_count":0,"width":554},{"aspect_ratio":0.69,"file_path":"/cHux0aJqxxvMT6MgVnMaLMXxnwz.jpg","height":2882,"id":"52bdff9019c2955ab21272f5","iso_639_1":"pl","vote_average":0.0,"vote_count":0,"width":2000},{"aspect_ratio":0.68,"file_path":"/p14JxT0fzvbeOv9fCAI67x0wuQ5.jpg","height":1469,"id":"52be5e01760ee37070015c47","iso_639_1":"pt","vote_average":0.0,"vote_count":0,"width":1000},{"aspect_ratio":0.7,"file_path":"/heF6qrZDSrTbfFs0mRlj9imJJKN.jpg","height":1428,"id":"52f103f919c295253e05d5a2","iso_639_1":"es","vote_average":0.0,"vote_count":0,"width":1000},{"aspect_ratio":0.69,"file_path":"/rcA5RpSJI2cHSgnppvI3locslbY.jpg","height":2048,"id":"53a13c29c3a3687bbc001b22","iso_639_1":"tr","vote_average":0.0,"vote_count":0,"width":1421},{"aspect_ratio":0.7,"file_path":"/iB101OMznrVlyGq84f4jwg49VVF.jpg","height":966,"id":"53be4b3ac3a3687e59002922","iso_639_1":"ko","vote_average":0.0,"vote_count":0,"width":678}]}'; // convert it to an array enabling the second parameter (associative array) $array = json_decode($json, true); // this is the whole array var_dump($array); // this is the key you need to get var_dump($array["backdrops"][0]["file_path"]); // which is similar to (the internal pointer points to the first element): $sub_array = current($array["backdrops"]); var_dump($sub_array["file_path"]);
How to get data from an array inside json data php
I have the following array of json data. When I try to loop using a for each, php gives me the error "Object of class stdClass could not be converted to string". I want to access the nameserver in the authns array, and the soa array. How do I go about doing that? stdClass Object ( [error] => [domain] => whoisdoma.net [ip_address] => 108.162.199.120 [authns] => stdClass Object ( [nameserver] => uma.ns.cloudflare.com [ip] => 173.245.58.146 [location] => San Francisco ) [soa] => stdClass Object ( [nameserver] => tom.ns.cloudflare.com [email] => dns.cloudflare.com [serial] => 2015505396 [refresh] => 10000 [retry] => 2400 [expiry] => 604800 [minimum] => 3600 ) ) This is the code I'm using to get the data: <?php $domain = $_GET['domain']; //set the url $url = "http://api.site.ga/v1/dns/lookup?domain=".$domain; //start a curl session $ch = curl_init(); $timeout = 5; //set curl options curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); //get the data $data = curl_exec($ch); //decode the data $jsonData = json_decode($data); //close the curl session curl_close($ch); ?> This is the for each loop I'm using <?php foreach($jsonData->authns as $authns) { echo $authns->nameserver; } ?>
You need to decode the json object using an associative array. Here is an exmaple: $data = json_decode(file_get_contents('http://api.domain.com/call'), true); $data is now an associative array.
You are looping through authns while it is an object by itself. So your nameserver which you want is actually $authns in its first iteration. Just access it like $jsonData->authns->nameserver try this for yourself foreach($jsonData->authns as $authns) { echo $authns; }