<?php
class CurlRequest
{
private $ch;
/**
* Init curl session
*
* $params = array('url' => '',
* 'host' => '',
* 'header' => '',
* 'method' => '',
* 'referer' => '',
* 'cookie' => '',
* 'post_fields' => '',
* ['login' => '',]
* ['password' => '',]
* 'timeout' => 0
* );
*/
public function init($params)
{
$this->ch = curl_init();
$user_agent = 'Mozilla/5.0 (Windows; U;Windows NT 5.1; ru; rv:1.8.0.9) Gecko/20061206 Firefox/1.5.0.9';
$header = array(
"Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5", "Accept-Language: ru-ru,ru;q=0.7,en-us;q=0.5,en;q=0.3","Accept-Charset: windows-1251,utf-8;q=0.7,*;q=0.7","Keep-Alive: 300");
if (isset($params['host']) && $params['host']) $header[]="Host: " . $params['host'];
if (isset($params['header']) && $params['header']) $header[]=$params['header'];
@curl_setopt ( $this -> ch , CURLOPT_RETURNTRANSFER , 1 );
@curl_setopt ( $this -> ch , CURLOPT_VERBOSE , 0 );
@curl_setopt ( $this -> ch , CURLOPT_HEADER , 1 );
if ($params['method'] == "HEAD") @curl_setopt($this -> ch,CURLOPT_NOBODY,1);
@curl_setopt ( $this -> ch, CURLOPT_FOLLOWLOCATION, 1);
@curl_setopt ( $this -> ch , CURLOPT_HTTPHEADER, $header );
if ($params['referer']) @curl_setopt ($this -> ch , CURLOPT_REFERER, $params['referer'] );
@curl_setopt ( $this -> ch , CURLOPT_USERAGENT, $user_agent);
if ($params['cookie']) @curl_setopt ($this -> ch , CURLOPT_COOKIE, $params['cookie']);
if ( $params['method'] == "POST" )
{
curl_setopt( $this -> ch, CURLOPT_POST, true );
curl_setopt( $this -> ch, CURLOPT_POSTFIELDS, $params['post_fields'] );
}
@curl_setopt( $this -> ch, CURLOPT_URL, $params['url']);
@curl_setopt ( $this -> ch , CURLOPT_SSL_VERIFYPEER, 0 );
@curl_setopt ( $this -> ch , CURLOPT_SSL_VERIFYHOST, 0 );
if (isset($params['login']) & isset($params['password']))
@curl_setopt($this -> ch , CURLOPT_USERPWD,$params['login'].':'.$params['password']);
@curl_setopt ( $this -> ch , CURLOPT_TIMEOUT, $params['timeout']);
}
/**
* Make curl request
*
* @return array 'header','body','curl_error','http_code','last_url'
*/
public function exec()
{
$response = curl_exec($this->ch);
$error = curl_error($this->ch);
$result = array( 'header' => '',
'body' => '',
'curl_error' => '',
'http_code' => '',
'last_url' => '');
if ( $error != "" )
{
$result['curl_error'] = $error;
return $result;
}
$header_size = curl_getinfo($this->ch,CURLINFO_HEADER_SIZE);
$result['header'] = substr($response, 0, $header_size);
$result['body'] = substr( $response, $header_size );
$result['http_code'] = curl_getinfo($this -> ch,CURLINFO_HTTP_CODE);
$result['last_url'] = curl_getinfo($this -> ch,CURLINFO_EFFECTIVE_URL);
return $result;
}
}
$curl = new CurlRequest();
$zip = array(90026, 90059, 90230, 91748, 90747, 91114, 92614, 92334,91770, 92840);
$dpci = array('057-10-1830', '057-10-1831', '057-10-1839', '057-10-1832', '057-10-1840', '057-10-1841');
$content = '<html>';
foreach($dpci as $ipad) {
foreach($zip as $z) {
try {
$params = array(
'url' => 'http://sites.target.com/site/en/spot/mobile_fiats_results.jsp?_DARGS=/site/en/spot/mobile_fiats.jsp',
'host' => '',
'header' => '',
'method' => 'POST', // 'POST','HEAD'
'referer' => '',
'cookie' => '',
'post_fields' => '_dyncharset=ISO-8859-1&asin=&dpci='.$ipad.'&zipcode='.$z.'&city=&state=', // 'var1=value&var2=value
'timeout' => 20
);
$curl->init($params);
$result = $curl->exec();
if ($result['curl_error']) throw new Exception($result['curl_error']);
if ($result['http_code']!='200') throw new Exception("HTTP Code = ".$result['http_code']);
if (!$result['body'])
throw new Exception("Body of file is empty");
$html = $result['body'];
$d = new domDocument;
@$d->loadHTML($html);
$list = $d->getElementsByTagName('ul');
$haystack = $list->item(1)->nodeValue;
if(stripos($haystack, 'ava')) {
$content .= '<h1>'.$ipad.' - '.$z.'</h1>';
$content .= trim($haystack);
}
} catch (Exception $e) {
echo $e->getMessage();
}
}
}
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
echo '<body>'.$content.'</body></html>';
//send output
//mail('your@gmail.com', 'target', $content, $headers);