Share the following page keywords to grab www.icbase.com site code of with asp.net parameters

  • 2020-12-20 03:30:42
  • OfStack


<?php
/**
 * HOST: www.icbase.com
 */
//set_time_limit(0);
// base function
function curl_get($url, $data = array(), $header = array(), $timeout = 15, $port = 80, $reffer = '', $proxy = '')
{
 $ch = curl_init();
 if (!empty($data)) {
 $data = is_array($data)?http_build_query($data): $data;
 $url .= (strpos($url,'?')? '&': "?") . $data;
 }
 curl_setopt($ch, CURLOPT_URL, $url);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
 curl_setopt($ch, CURLOPT_POST, 0);
 curl_setopt($ch, CURLOPT_PORT, $port);
 curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // Whether to grab the page after the jump 
 $reffer && curl_setopt($ch, CURLOPT_REFERER, $reffer);
 if($proxy) {
 curl_setopt($ch, CURLOPT_PROXY, $proxy);
 curl_setopt($ch, CURLOPT_PROXYPORT, 1723);
 curl_setopt($ch, CURLOPT_PROXYUSERPWD,"andhm001:andhm123");
 }

$result = array();
 $result['result'] = curl_exec($ch);
 if (0 != curl_errno($ch)) {
 $result['error'] = "Error:\n" . curl_error($ch);

}
 curl_close($ch);
 return $result;
}


function curl_post($url, $data = array(), $header = array(), $timeout = 5, $port = 80)
{
 $ch = curl_init();
 curl_setopt($ch, CURLOPT_URL, $url);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
 //curl_setopt($ch, CURLOPT_PORT, $port);
 !empty ($header) && curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
 curl_setopt($ch, CURLOPT_POST, 1);
 curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

$result = array();
 $result['result'] = curl_exec($ch);
 if (0 != curl_errno($ch)) {
 $result['error'] = "Error:\n" . curl_error($ch);

}
 curl_close($ch);

return $result;
}

/**
 *  Gets the list page of html The source code 
 * @param string $keywords  Search keywords 
 * @param int $page  Number of pages 
 * @return boolean|array
 */
function getListHtml($keywords, $page=1)
{
 if ($page < 0)
 {
 return false;
 }
 $page = $page == 0 ? 1 : intval($page);
 if ($page == 1)
 {
 $result = curl_get('http://www.icbase.com/ProResult.aspx', array('ProKey' => $keywords));
 if ( isset($result['error']) )
 {
 return false;
 //exit($result['error']);
 }
 $result = $result['result'];

 // asp.net post Submit data 
 if(! defined('__VIEWSTATE') && preg_match('/<input\s+type="hidden"\s+name="__VIEWSTATE"\s+id="__VIEWSTATE"\s+value="(.[^"]+)"/isU', $result, $matches))
 {
 define('__VIEWSTATE', $matches[1]);
 } else {
 return false;
 }

 if(! defined('__PREVIOUSPAGE') && preg_match('/<input\s+type="hidden"\s+name="__PREVIOUSPAGE"\s+id="__PREVIOUSPAGE"\s+value="(.[^"]+)"/isU', $result, $matches))
 {
 define('__PREVIOUSPAGE', $matches[1]);
 } else {
 return false;
 }

 if(! defined('__EVENTVALIDATION') && preg_match('/<input\s+type="hidden"\s+name="__EVENTVALIDATION"\s+id="__EVENTVALIDATION"\s+value="(.[^"]+)"/isU', $result, $matches))
 {
 define('__EVENTVALIDATION', $matches[1]);
 } else {
 return false;
 }

 return $result;
 }
 $data = array(
 '__EVENTTARGET' => 'pager',
 '__EVENTARGUMENT' => $page,
 '__VIEWSTATE' => __VIEWSTATE,
 '__PREVIOUSPAGE' => __PREVIOUSPAGE,
 '__EVENTVALIDATION' => __EVENTVALIDATION,
 );
 $result = curl_post('http://www.icbase.com/ProResult.aspx?ProKey=' . $keywords, $data);
 if ( isset($result['error']) )
 {
 return false;
 //exit($result['error']);
 }
 $result = $result['result'];
 return $result;
}

/**
 *  Get list page  a Of the link url
 * @param string $html html The source code 
 * @return array
 */
function getListHref($html)
{
 $pattern = '/<a\s+href=\'(.[^\']+)\'\s+target="_blank"\s*>[\s\n]*<img.+[^>]\/>/isU';
 if (preg_match_all($pattern, $html, $matches))
 {
 return $matches[1];
 } else {
 //  No match 
 return array();
 }
}

/**
 *  To obtain the 1 Page Numbers 
 * @param string $html html The source code 
 * @return number
 */
function getListNextPage($html)
{
 $pattern = '/<div\s+id="Pager".+[^>]>.+<a\s+href="javascript\:__doPostBack\(\'Pager\',\'(\d+)\'\)">><\/a>/isU';
 if (preg_match($pattern, $html, $matches))
 {
 return intval($matches[1]);
 } else {
 return -1;
 }
}

/**
 *  Getting the list is also all href
 * @param string $keywords  Search keywords 
 * @return boolean|array
 */
function getListHrefAll($keywords)
{
 if (empty($keywords))
 {
 return false;
 }

 $html = getListHtml($keywords);
 $hrefList = getListHref($html);
 if (empty($hrefList))
 {
 //  No results 
 return array();
 }
 $nextPage = getListNextPage($html);
 while ($nextPage > 0)
 {
 $html = getListHtml($keywords, $nextPage);
 $tmpHrefList = getListHref($html);
 $hrefList = array_merge($hrefList, $tmpHrefList);
 $nextPage = getListNextPage($html);
 }
 return $hrefList;
}

/**
 *  Get details page information 
 * @param string $url url The address is either grabbed html The source code   According to the @see $is_url  Distinguish between 
 * @param int $is_url 1 Using the url address  0 Deal directly with html The source code 
 * @return boolean|multitype:|multitype:string
 */
function getDetail($url, $is_url = 1)
{
 if ( empty($url) )
 {
 return false;
 }
 $host = 'www.icbase.com';
 $html = $url;
 if ($is_url) {
 $url = '/' . ltrim($url, '/');
 $result = curl_get($host . $url);
 if ( isset($result['error']) )
 {
 exit($result['error']);
 }
 $html = $result['result'];
 }

 $result = array(
 'sup_part' => '', //  Supplier model 
 'sup_id' => '', //  supplier ID
 'mfg_part' => '', //  Manufacturer model 
 'mfg_name' => '', //  Manufacturer's name 
 'cat_name' => '', //  Category name 
 'para' => '', //  attribute 
 'desc' => '', //  describe 
 'pdf_url' => '', // PDF address 
 'sup_stock' => '', //  inventory 
 'min_purch' => '', //  Minimum order quantity 
 'price' => '', //  The price 
 'img_url' => '', //  Picture address 
 'createtime' => '', //  Creation time 
 'datacode' => '', //  Batch number 
 'package' => '', //  encapsulation 
 'page_url' => '', //  Page address 
 );

// mfg_part
 $pattern = '/<td> Product model <\/td><td>(.[^<]+)</isU';
 if (preg_match($pattern, $html, $matches))
 {
 $result['mfg_part'] = trim($matches[1]);
 } else {
 //  This item does not have, explanation also does not have everywhere 
 return array();
 }

 // mfg_name
 $pattern = '/<td> vendor <\/td>[\s\n]*<td>(.+)<\/td>/isU';
 if (preg_match($pattern, $html, $matches))
 {
 $result['mfg_name'] = trim($matches[1]);
 }

 // para
 $pattern = '/<tr\s+style="background-color:#E9E9E9;color:black; font-weight:bold;">(.+)<\/tr><\/table>/isU';
 if (preg_match($pattern, $html, $matches))
 {
 if (preg_match_all('/<td>(.+)<\/td>/isU', $matches[1], $matches))
 {
 $count = count($matches[1]);
 $count = intval($count / 2 );
 foreach ($matches[1] as $k=>$v)
 {
 if ($k >= $count)
 {
 break;
 }
 if (trim($v) == ' describe ')
 {
 // desc
 $result['desc'] = trim($matches[1][$count + $k]);
 continue;
 }
 $v = trim($v);
 $result['para'][$v] = trim($matches[1][$count + $k]);
 }
 }
 }

 // pdf_url
 $pattern = '/<td> Detailed information on <\/td><td><a\s+href="(.[^"]+)"/isU';
 if (preg_match($pattern, $html, $matches))
 {
 $result['pdf_url'] = trim($matches[1]);
 }

 // sup_stock
 $pattern = '/<td> Inventory quantity <\/td>[\s\n]*<td>(\d+)<\/td>/isU';
 if (preg_match($pattern, $html, $matches))
 {
 $result['sup_stock'] = trim($matches[1]);
 }

 // price
 $pattern = '/<tr><td.[^>]+>(\d+)\+<\/td><td.[^>]+>.[^\d]*([\d.]+)<\/td><\/tr>/isU';
 if (preg_match_all($pattern, $html, $matches))
 {
 foreach ($matches[1] as $k=>$v)
 {
 $result['price'][$v] = ' RMB ' . $matches[2][$k];
 }
 }

 //img_url
 $pattern = '/<td> The picture <\/td><td><img\s+src="(.[^"]+)"/isU';
 if (preg_match($pattern, $html, $matches))
 {
 $result['img_url'] = trim($matches[1]);
 }

 // page_url
 if ($is_url)
 {
 $result['page_url'] = $host . $url;
 }

return $result;
}

/**
 *  Final call function 
 * @param string $keywords  Search keywords 
 * @return array
 */
function getData($keywords)
{
 $hrefList = getListHrefAll($keywords);
 $result = array();

 foreach ($hrefList as $k=>$v)
 {
 $result[] = getDetail($v);
 }

 return $result;
}

// Test Script
$keywords = trim($_GET['keywords']);
$result = getData($keywords);

print_r($result);


Related articles: