Skip to content

Instantly share code, notes, and snippets.

View Tonel's full-sized avatar

Antonello Zanini Tonel

View GitHub Profile
<?
curl_setopt($curl, CURLOPT_HTTPHEADER,
array(
"<HTTP_HEADER_1>: <HEADER_VALUE>",
"<HTTP_HEADER_2>: <HEADER_VALUE>",
// ...
)
);
<?
curl_setopt($curl, CURLOPT_USERAGENT, "<USER_AGENT_STRING>");
[
{
"url": "https://scrapeme.live/shop/Bulbasaur/",
"image": "https://scrapeme.live/wp-content/uploads/2018/08/001-350x350.png",
"name": "Bulbasaur",
"price": "£63.00"
},
...
<?
// iterate over all "/shop/page/X" pages and retrieve all product data
for ($paginationNumber = 1; $paginationNumber <= $highestPaginationNumber; $paginationNumber++) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "https://scrapeme.live/shop/page/$paginationNumber/");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$pageHtml = curl_exec($curl);
curl_close($curl);
$paginationHtmlDomParser = HtmlDomParser::str_get_html($pageHtml);
<?
$productDataLit = array();
// retrieve the list of products on the page
$productElements = $htmlDomParser->find("li.product");
foreach ($productElements as $productElement) {
// extract the product data
$url = $productElement->findOne("a")->getAttribute("href");
$image = $productElement->findOne("img")->getAttribute("src");
$name = $productElement->findOne("h2")->text;
<?
// retrieve the HTML pagination elements with
// the ".page-numbers a" CSS selector
$paginationElements = $htmlDomParser->find(".page-numbers a");
$paginationLinks = [];
foreach ($paginationElements as $paginationElement) {
// populate the paginationLinks set with the URL
// extracted from the href attribute of the HTML pagination element
$paginationLink = $paginationElement->getAttribute("href");
// avoid duplicates in the list of URLs
<?
require_once __DIR__ . "/../../vendor/autoload.php";
use voku\helper\HtmlDomParser;
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "https://scrapeme.live/shop/");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
$html = curl_exec($curl);
@Tonel
Tonel / curl.php
Last active August 13, 2022 11:54
<?
// initialize the cURL request
$curl = curl_init();
// set the URL to reach with a GET HTTP request
curl_setopt($curl, CURLOPT_URL, "https://scrapeme.live/shop/");
// get the data returned by the cURL request as a string
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
// make the cURL request follow eventual redirects,
// and reach the final page of interest
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
{
"name": "Maria",
"surname": "Williams",
"age": 35
}