京东联盟开发(7)——判断是否有货

 

<?php
// header("Content-type: text/html; charset=utf-8");
  

$result2 = http_request("https://item.m.jd.com/product/4914531.html");
 
$cat11 = iconv("BIG5", "UTF-8//IGNORE", $result2);
var_dump($cat11);  


        $url = "http://item.jd.com/5544082.html";
        var_dump($url);
        $headers = array(
        "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36",
        "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
        "Accept-Language: zh-CN,zh;q=0.8,en;q=0.6,zh-TW;q=0.4",
        "Origin: https://www.jd.com/",
        "Referer: https://www.jd.com/",
        );
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        // curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER,false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,false);
        $output = curl_exec($curl);
        curl_close($curl);

        // var_dump($output);
        preg_match('/mbNav-1">(.*?)<\/a>/si', $output, $cat1);
        preg_match('/mbNav-2">(.*?)<\/a>/si', $output, $cat2);
        preg_match('/mbNav-3">(.*?)<\/a>/si', $output, $cat3);
        preg_match('/mbNav-3">(.*?)<\/a>/si', $output, $cat3);
        
        $express_youhuo = '/<a\shref="#(.+)".+>加入购物车<\/a>/i';
        preg_match($express_youhuo, $output, $cat4);
        var_dump($cat4);

        // var_dump($cat1[1]);
                $encode = mb_detect_encoding($cat1[1], array("ASCII","UTF-8","GB2312","GBK","BIG5"));
                var_dump($encode);
                // var_dump($data);


        // var_dump($result);
        if (isset($cat1[1]) && isset($cat2[1]) && isset($cat3[1]) && !empty($cat1[1])){
            $cat11 = iconv("GBK", "UTF-8//IGNORE", $cat1[1]);
            $cat11 = str_replace("、", "", $cat11);
            $cat21 = iconv("GBK", "UTF-8//IGNORE", $cat2[1]);
            $cat31 = iconv("GBK", "UTF-8//IGNORE", $cat3[1]);
            if (isset($cat4[1])){
                $category = array($cat11, $cat21, $cat31, 0);   //无货
            }else{
                $category = array($cat11, $cat21, $cat31, 1);//有货
            }
            
        }else{
            $category = array("海外", "NULL", "NULL", 1); //海外默认有货
        }
       
        var_dump($category);
 
function http_request($url, $info = False)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    $output = curl_exec($ch);
    if ($info){
        $info = curl_getinfo($ch);
        // $retURL = $info['url'];
        curl_close($ch);
        return $info;
    }else{
        curl_close($ch);
        return $output;
    }
}
?>

 

posted @ 2019-07-11 16:09  方倍工作室  阅读(582)  评论(0编辑  收藏  举报