Jump to content

Extracting Data from an API


Recommended Posts

I am trying to extract data from an API and not to sure where I am going wrong or even doing it correctly for that matter.  Initally I cannot even print out the URL values

 

Any help would be greatly appreciated professionals.

 

here is my code, i know the url works in the browser address bar

 

 

 

 
<?php 
 
 
 
 
 
    $holidays_curl = curl_init(); 
    curl_setopt($holidays_curl, CURLOPT_ENCODING, "gzip");
    curl_setopt($holidays_curl, CURLOPT_URL, $holidays_url);
    curl_setopt($holidays_curl, CURLOPT_RETURNTRANSFER, true);  
    $holidays_xml = new SimpleXMLElement(curl_exec($holidays_curl));
    $holidays_date = DateTime::createFromFormat("Y-m-d", $startdate);
 
 
    $holidays_link=$holidays_xml;
 
    $holidays_stdclass=json_decode(json_encode($holidays_xml));
echo "<pre>";
print_r($holidays_xml);
 
?>
 
Link to post
Share on other sites
 


 

    $holidays_curl = curl_init(); 

    curl_setopt($holidays_curl, CURLOPT_ENCODING, "gzip");

    curl_setopt($holidays_curl, CURLOPT_URL, $holidays_url);

    curl_setopt($holidays_curl, CURLOPT_RETURNTRANSFER, true);  

    $holidays_xml = new SimpleXMLElement(curl_exec($holidays_curl));

 

 

 

    $holidays_stdclass=json_decode(json_encode($holidays_xml));

error_reporting(E_ALL);

 

ini_set("display_errors", 1);

echo "<pre>";

print_r($holidays_stdclass);

 

 

 

This still shows a blank screen

Link to post
Share on other sites

ok i have this now

 

 

ini_set("display_errors", 1);
error_reporting(E_ALL);
 
 
    $holidays_curl = curl_init($holidays_url); 
    curl_setopt($holidays_curl, CURLOPT_ENCODING, "gzip");
    curl_setopt($holidays_curl, CURLOPT_URL, $holidays_url);
    curl_setopt($holidays_curl, CURLOPT_RETURNTRANSFER, true);  
    //$holidays_xml = new SimpleXMLElement(curl_exec($holidays_curl));
 
$holidays_xml = new SimpleXMLElement(curl_exec($holidays_curl),null,true);
//echo $feed->SHOPITEM[0]->ID_PRODUCT;
 
    $holidays_stdclass=json_decode(json_encode($holidays_xml));
 
echo "<pre>";
 
print_r($holidays_stdclass);
 
 
 
 
i am getting error message
 
SimpleXMLElement::__construct(): I/O warning : failed to load external entity "" in /home/ccuk/public_html/holiday-test.php on line 53

Fatal error: Uncaught Exception: String could not be parsed
Link to post
Share on other sites

Alright, try to change:

curl_setopt($holidays_curl, CURLOPT_RETURNTRANSFER, true);

into:

 

curl_setopt($holidays_curl, CURLOPT_RETURNTRANSFER, true);
$debugme=curl_exec($holidays_curl);
echo $debugme; exit();
What do you see? Hint: you may need to click CTRL + U to view the source code of the output because browser itself surely displays an empty page (because result is XML tags). Do you at least see some output returned?
Link to post
Share on other sites

I see no output still

 

this is what i now have

 

ini_set("display_errors", 1);
error_reporting(E_ALL);
 
 
    $holidays_curl = curl_init($holidays_url); 
    curl_setopt($holidays_curl, CURLOPT_ENCODING, "gzip");
    curl_setopt($holidays_curl, CURLOPT_URL, $holidays_url);
curl_setopt($holidays_curl, CURLOPT_RETURNTRANSFER, true);
$debugme=curl_exec($holidays_curl);
echo $debugme; exit();
    //$holidays_xml = new SimpleXMLElement(curl_exec($holidays_curl));
 
$holidays_xml = new SimpleXMLElement(curl_exec($holidays_curl),null,true);
 
$holidays_stdclass=json_decode(json_encode($holidays_xml));
 
echo "<pre>";
print_r($holidays_stdclass);
 
 
 
and not even an error message now?
 
i know when i click the url i can see results but can seem to extract info from it
Link to post
Share on other sites

Here, start with something that gets the data.

<?php
function get_data($path){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$path);
    curl_setopt($ch, CURLOPT_FAILONERROR,1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 15);
    $retValue = curl_exec($ch);
    curl_close($ch);
    return $retValue;
}

$a = get_data('http://87.102.127.86:8103/search/searchoffers.dll?page=Resort&countryid=7');
$b = new SimpleXMLElement($a);

echo "<pre>";
print_r($b);
echo "</pre>";
Link to post
Share on other sites

hi there

 

I have tried that and get this error

 

Fatal error: Uncaught Exception: String could not be parsed as XML in /home/ccuk/public_html/holiday-test.php:18 Stack trace: #0 /home/ccuk/public_html/holiday-test.php(18): SimpleXMLElement->__construct('') #1 {main} thrown in /home/ccuk/public_html/holiday-test.php on line 18

Link to post
Share on other sites

benanamen's code works perfectly for me. If you don't see any output using my code too, I only have these ideas:

 

1. Your server can't connect to 87.102.127.86 for some reason such as invalid configuration in your end (you will need to find the reason on your own, however)

2. Your server has outgoing port 8103 blocked, so script can't parse data.

3. Your server is blocked by 87.102.127.86 administrator.

 

In other words, your server can't parse data because it can't fetch data. Because exactly the same error will be displayed if you try to pass an empty string into SimpleXMLElement function.

Edited by phpmillion
  • Like 1
Link to post
Share on other sites
This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.