Jeff
Jeff

Reputation: 29

Get data from the website

I have my own external website and I want to get some data from the website. I used CURL to get the content of the website however I want some part that is :

Edited : Very frankly speaking, I want to get the TimeStamp of the facebook page, If you use Inspect element on the page, you will see the code like this :

<span class="fsm fwn fcg"><a class="_5pcq">
<abbr title="Tuesday, June 30, 2015 at 5:00pm" data-utime="1435663826" data-shorten="1" class="_5ptz timestamp livetimestamp">5 hrs</abbr></a>
<span class="fsm fwn fcg"><a class="_5pcq">
<abbr title="Tuesday, June 30, 2015 at 5:01pm" data-utime="1435663827" data-shorten="1" class="_5ptz timestamp livetimestamp">5 hrs</abbr></a>
<span class="fsm fwn fcg"><a class="_5pcq">
<abbr title="Tuesday, June 30, 2015 at 5:02pm" data-utime="1435663828" data-shorten="1" class="_5ptz timestamp livetimestamp">5 hrs</abbr></a>
<span class="fsm fwn fcg"><a class="_5pcq">
<abbr title="Tuesday, June 30, 2015 at 5:03pm" data-utime="1435663829" data-shorten="1" class="_5ptz timestamp livetimestamp">5 hrs</abbr></a>
<span class="fsm fwn fcg"><a class="_5pcq">
<abbr title="Tuesday, June 30, 2015 at 5:04pm" data-utime="1435663830" data-shorten="1" class="_5ptz timestamp livetimestamp">5 hrs</abbr></a>
</span>

I just want to display the value of "data-utime" that is 1435663826. Here is my code which will fetch the contents. What should I use after this ?

 $cookie = tmpfile();
    $userAgent = 'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31' ;

    $ch = curl_init("https://www.mywebsite.com");

    $options = array(
        CURLOPT_CONNECTTIMEOUT => 20 , 
        CURLOPT_USERAGENT => $userAgent,
        CURLOPT_AUTOREFERER => true,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_COOKIEFILE => $cookie,
        CURLOPT_COOKIEJAR => $cookie ,
        CURLOPT_SSL_VERIFYPEER => 0 ,
        CURLOPT_SSL_VERIFYHOST => 0
    );

    curl_setopt_array($ch, $options);
    $kl = curl_exec($ch);
    curl_close($ch);

    echo $kl; // Final output after fetching

Upvotes: 1

Views: 571

Answers (2)

Josua M C
Josua M C

Reputation: 3158

If you already get the html tag you can

Try this:

<?php 

$curl = curl_init('https://www.facebook.com/Rajnikant.Vs.CIDJokez'); 


curl_setopt($curl, CURLOPT_FAILONERROR, true); 
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); 
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);   
$result = curl_exec($curl); 
//echo $result; 

/* $result = 
'<span class="fsm fwn fcg"><a class="_5pcq">
<abbr title="Tuesday, June 30, 2015 at 5:00pm" data-utime="1435663826" data-shorten="1" class="_5ptz timestamp livetimestamp">5 hrs</abbr></a>
<span class="fsm fwn fcg"><a class="_5pcq">
<abbr title="Tuesday, June 30, 2015 at 5:01pm" data-utime="1435663827" data-shorten="1" class="_5ptz timestamp livetimestamp">5 hrs</abbr></a>
<span class="fsm fwn fcg"><a class="_5pcq">
<abbr title="Tuesday, June 30, 2015 at 5:02pm" data-utime="1435663828" data-shorten="1" class="_5ptz timestamp livetimestamp">5 hrs</abbr></a>
<span class="fsm fwn fcg"><a class="_5pcq">
<abbr title="Tuesday, June 30, 2015 at 5:03pm" data-utime="1435663829" data-shorten="1" class="_5ptz timestamp livetimestamp">5 hrs</abbr></a>
<span class="fsm fwn fcg"><a class="_5pcq">
<abbr title="Tuesday, June 30, 2015 at 5:04pm" data-utime="1435663830" data-shorten="1" class="_5ptz timestamp livetimestamp">5 hrs</abbr></a>
</span>';
*/
$html = $result;
$dom = new DOMDocument();

@$dom->loadHTML($html);
$a = $dom->getElementsByTagName('abbr');

$data = array();

for ($i=0; $i < $a->length; $i++) {
    $data[] = $a->item($i)->getAttribute('data-utime');

}

echo '<pre>';
print_r($data);
echo '</pre>';

Upvotes: 0

VolkerK
VolkerK

Reputation: 96169

You could use PHP's DOM extension to load and parse the html document and then use an instance of DOMXPath to query the specific element.

Upvotes: 1

Related Questions