Skip to content

Instantly share code, notes, and snippets.

@theskillwithin
Created April 17, 2016 00:06
Show Gist options
  • Save theskillwithin/1d7b097feb6a78dbe28d70e5f8866058 to your computer and use it in GitHub Desktop.
Save theskillwithin/1d7b097feb6a78dbe28d70e5f8866058 to your computer and use it in GitHub Desktop.
<?php
function Amazon($ASIN) {
//$this->_ASIN = $ASIN;
$url = "http://www.amazon.com/dp/" . $ASIN;
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// grab URL and pass it to the browser
$html = curl_exec ($ch);
/* Clean-up */
$html = str_replace("&nbsp;", " ", $html);
//<li id="SalesRank"> <b>Amazon Best Sellers Rank:</b> #125,047 in Beauty';
function domdom($html) {
$doc = new DomDocument;
$doc->validateOnParse = true;
libxml_use_internal_errors(true);
$doc->LoadHTML($html);
$doc->preserveWhiteSpace = false;
$salesRankId = $doc->getElementById("SalesRank")->nodeValue;
return $salesRankId;
}
function pregs($html, $ASIN) {
$rel = '[#][0-9,]+'; //$re1='(?<=#)'; $re2='(.*)'; $re3='(?= in Beauty \(<a )';
if ($c=preg_match_all ("/".$rel."/is", $html, $matches))
{
$tag1=$matches[0][0] . " | " . $matches[0][1]; //$tag1 = preg_replace('/\s+/', '', $tag1); $tag1 = trim($tag1, '#'); $tag1 = trim($tag1, '/,'); $tag1 = preg_replace('/,/', '', $tag1); $tag1 = (int) $tag1; $tag1 = print_r($tag1);
}
return "$ASIN: $tag1 \n";
}
//return(pregs($html, $ASIN));
return( pregs ( domdom($html), $ASIN) );
}
echo "ASIN:Ranking\n";
$output = Amazon('B0001EL4QO'); // http://php.net/manual/en/function.array-map.php
echo $output;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment