So i'm trying to display a div from another page (For example Kinguin) with as mentioned in the title PHP cURL. Now I've figured out a way to do this with an image (Simple YT tutorial) with an image but i wasn't able to do this with a div that has a class bound to it. Some support page seems to head me to the right direction, but after a while it seems to get to complicated.
Is the way i'm heading the right one or should i use AJAX for example.
<?php
$curl = curl_init();
$search_string = "gta5";
$url = "https://www.kinguin.net/catalogsearch/result/index/?q=$search_string";
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_COOKIE,true); //Verify cookies
$result = curl_exec($curl);
preg_match_all("!https://cdns.kinguin.net/media/catalog/category/cache/1/image/173x118/9df78eab33525d08d6e5fb8d27136e95/gta5_12.jpg!", $result,$matches);
$images = array_values(array_unique($matches[0]));
for ($i = 0; $i < count ($images); $i++){
echo "<div style='float: left; margin: 10 0 0 0; '> ";
echo "<img src='$images[$i]'><br />";
echo "</div>";
}
curl_close($curl);
?>
For parsing the div elements, you can use a parser. There are plenty of libraries out there. One is SimpleHtml Dom. Which has selector features like the following:
// Find all images
foreach($html->find('img') as $element) echo $element->src . '<br>';
// Find all links
foreach($html->find('a') as $element) echo $element->href . '<br>';
Some JQuery selector styled php html parsers are also available. One is (but I have not used it yet) at: https://github.com/tburry/pquery
You can also go the AJAX way in the front site, but in that case you will have to use jsonp since the domain is different. You can fetch the html via jsonp, put it inside a hidden container in the page for temporary, and parse the data from this container.
My preference would be to do the task in serverside. Reasons are:
a) It will not put pressure on the client computer/device.
As the browser page will have to pull data from another domain.
b) You can cache the data in server.
c) Parsing is costly. In some device the browser may go irresponsive.
d) In serverside you will be able handle the exceptions(if any occurs, like page unavailable, html structure of that page got changed etc) better than in a client's browser.
The solution here really depends on what content you want to pull down. preg_match_all
is a string matching function which uses Regular Expressions. You can find the documentation for it here, and you can test regular expressions with browser-apps like RegExr Your adjusted preg_match_all
call might look something like this:
preg_match_all('@<div class="some-class">[^<]+</div>@', $result, $matches);
However, since you are pulling down a <div>
tag and presumably all of its content, you may want to look into html parsing libraries like DOMDocument:
$dom = new DOMDocument(); $dom->loadHTML($result);
foreach ($dom->getElementsByTagName('div') as $div) {
$class = $item->getAttribute("class");
if (strpos($class, 'some-class') !== false) {
echo "<div>";
echo $div->nodeValue;
echo "</div>";
}
}
If you don't want to use DOMDocument (understandable, considering it's designed more for XML), then try looking for composer libraries. https://packagist.org/?query=html%20parser