Using PHP cURL to get some text from website and store in MySQL

Kleidi :

I have been looking around for a while to make this work but seems that I can't do it by myself. I am using cURL to get some informations from an website and store those infos on MySQL database. What I have right now is following code :

$target_url = "[http:\[//\]iliria98\[.\]com][1]"; //delete [ and ] to get the url correctly
$userAgent = 'Googlebot/2.1 (http://www.googlebot.com/bot.html)';

// make the cURL request to $target_url
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
curl_setopt($ch, CURLOPT_URL,$target_url);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_TIMEOUT, 100);
$html= curl_exec($ch);
if (!$html) {
    echo "<br />cURL error number:" .curl_errno($ch);
    echo "<br />cURL error:" . curl_error($ch);
    exit;
}

// parse the html into a DOMDocument
$document = new DOMDocument();
libxml_use_internal_errors(true);
$document->loadHTML($html);
libxml_clear_errors();
$selector = new DOMXPath($document);
//$anchors = $selector->query('//div[@class="single"]/div[2]');
$anchors = $selector->query('//div[@class="single"]/div');
foreach($anchors as $div) { 
    $text = $div->nodeValue;

    $valuta_arr=explode(',', $text);
    var_dump($valuta_arr);
    echo $text;
}

And, the output is not a correct one, since it gets all te currency codes from the website, but the currency values are only from the first rows, from USD. What I want is to get the values from the html table on the url specified and to insert those values in the database for every currency, where the database table looks like this:

id
currency
sell
buy
date

I didn't get till the mysql insert code since I have been struggling for 3 days to firstly get the informations fro that website. Hope that someone can help me on this. Thank you to everyone.

Eugene Ruban :

if you will try to get this page from the console by curl http://iliria98.com you will find, that this widget is filled by js-script:

$('div#usd1').append('<div style="position: absolute; background: transparent; width: 100%; height: 100%; left: 0; top: 0; z-index: 9999;"></div>')
$(".kursiweb .single").eq(0).find("div").eq(1).html("114<sup>.20</sup>");  $(".kursiweb .single").eq(0).find("div").eq(2).html("116");

and etc...

So, you can get needed data only from this script in source HTML you get from curl, not from the DOM document, just because curl didn't have any JS engine.

Another way you can go - to use something like PhantomJS

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=379608&siteId=1