时间:2021-07-01 10:21:17 帮助过:3人阅读
代码如下:
function curl_post($url, $data = array(), $header = array(), $timeout = 15, $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;
}
/**
* 获取列表页的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 = '/