代码如下:
PHP
/**
* HOST: components.arrow.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); //是否抓取跳转后的页面
$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;
}
/**
* HOST: components.arrow.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); //是否抓取跳转后的页面
$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,$port = 80)
{
$ch = curl_init();
curl_setopt($ch,$port);
!empty ($header) && curl_setopt($ch,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;
}/**
* 获取列表页的html源码
* @param string $keywords 搜索关键字
* @param int $start 开始记录数
* @return boolean|array
*/
function getListHtml($keywords,$start = 0)
{
if ($start < 0)
{
return false;
}$postData = array(
'search_token' => $keywords,
'start' => $start,
'limit' => 100,
);$result = curl_post('http://components.arrow.com/part/search/' . $keywords,http_build_query($postData));
if ( isset($result['error']) )
{
return false;
//exit($result['error']);
}
$result = $result['result'];return $result;
}/**
* 获取列表页 连接href
* @param string $html html源码
* @return array
*/
function getListHref($html)
{
$pattern = '/]+)">/isU';
if (preg_match_all($pattern,$html,$matches))
{
return $matches[1];
} else {
// 没有匹配项
return array();
}
}/**
* 获取下一页数字start
* @param string $html html源码
* @return number
*/
function getListNextPage($html)
{
$pattern = '/
{
$ch = curl_init();
curl_setopt($ch,$port);
!empty ($header) && curl_setopt($ch,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;
}/**
* 获取列表页的html源码
* @param string $keywords 搜索关键字
* @param int $start 开始记录数
* @return boolean|array
*/
function getListHtml($keywords,$start = 0)
{
if ($start < 0)
{
return false;
}$postData = array(
'search_token' => $keywords,
'start' => $start,
'limit' => 100,
);$result = curl_post('http://components.arrow.com/part/search/' . $keywords,http_build_query($postData));
if ( isset($result['error']) )
{
return false;
//exit($result['error']);
}
$result = $result['result'];return $result;
}/**
* 获取列表页 连接href
* @param string $html html源码
* @return array
*/
function getListHref($html)
{
$pattern = '/
if (preg_match_all($pattern,$html,$matches))
{
return $matches[1];
} else {
// 没有匹配项
return array();
}
}/**
* 获取下一页数字start
* @param string $html html源码
* @return number
*/
function getListNextPage($html)
{
$pattern = '/