很多做淘宝客的朋友会用到采集,那么怎么提取出淘宝商品id呢

时间:2021-01-07 23:14:00

function hq_taoid($taourl) {//根据淘宝地址获取淘宝商品id
$strurl = strtolower ( $taourl);
if (strpos ( $strurl, 'id' ) !== false) {
$arr = explode ( '?', $taourl );
$arr = explode ( '&', $arr [1] );
$NO = 0;
foreach ( $arr as $k => $v ) {
if (is_string ( $v )) {
//判断是否含有id
if (strpos ( $v, 'id' ) !== false) {
//处理含有item或者num项 返还id数
if (strpos ( $v, 'item' ) !== false || strpos ( $v, 'num' ) !== false) {
//echo $v,'<br/>';
$i = strrpos ( $v, '=' );
$str = substr ( $v, $i + 1 );
if (is_numeric ( $str )) {
return $NO = $str;
}
} else {
//echo $v,'<br/>';
$i = strrpos ( $v, '=' );
$str = substr ( $v, $i + 1 );
$x = strlen ( $str );
if (is_numeric ( $str )) {
if ($x ==11) {
$NO = $str;
} else if ($NO == 0 || ($x > 9 && $x < 11)) {
$NO = $str;
}
}
}
}
}
}
return $NO;
}
}
代码放到php里面,找到一个淘宝商品地址测试即可