In this article you'll see examples of using proxy, proxy authentication, scrapping, and parsing data using regular expressions.
Note: this script was created for educational purposes only. Alexa provides really cheap service (1000 images for $0.20), so please do not overuse use this script, and use in educational purposes only.
1. Save php code provided below in some php file, for example alexa_image.php.
2. Create cron job to run this script each maybe 5 minutes (as long as session does not expire). It will create file named
image_url.txt, containing sample image url (template) with recent session id.
3. Create another script, where you will show images. Read image string from image_url.txt, replace cnn.com with with your desired site, and use it in
img tag, like:
<img src="here image string you read from file">
There is
Size=Large parameter, which can be tuned to alter image size (Large or Small).
Tip: you could
generate thumbnails yourself on Windows server.
<?php
// use proxy?
$use_proxy = false;
// following only needed when using proxy:
$proxy_auth = true; // proxy authentication?
$proxy_name = '23.23.23.23'; // proxy ip
$proxy_port = 8080;
$proxy_user = "user";
$proxy_pass = "password";
// url to grab alexa image path from
$url = "http://www.alexa.com/data/details/traffic_details?&range=1y&size=large&compare_sites=&url=www.cnn.com";
if ($use_proxy) {
$fp = fsockopen($proxy_name, $proxy_port);
if (!$fp) {return false;}
fputs($fp, "GET $url HTTP/1.0\r\nHost: $proxy_name\r\n");
if ($proxy_auth)
fputs($fp, "Proxy-Authorization: Basic " . base64_encode ("$proxy_user:$proxy_pass") . "\r\n");
fputs($fp, "\r\n");
}
else {
$tmp = parse_url($url);
if (empty($tmp['port'])) {
$tmp['port'] = 80;
}
$fp = fsockopen($tmp['host'], $tmp['port'], $errno, $errstr);
$path = $tmp['path'];
if (isset($tmp['query'])) { $path .= "?$tmp[query]"; }
fputs($fp, "GET $path HTTP/1.0\r\nHost: $tmp[host]:$tmp[port]\r\n\r\n");
}
while(!feof($fp)) {
$content = fread($fp,4096);
$found = preg_match('/(http:\/\/ast.amazonaws.com\/Xino[^"]*)/', $content, $match);
if ($found) {
$content = $match[1];
break;
}
else {
$content = '';
}
}
fclose($fp);
// save image url to file
$fp = fopen("image_url.txt",'w+');
fputs($fp,$content);
fclose($fp);
?>