美文网首页
php采集天猫商城详细

php采集天猫商城详细

作者: LauEl | 来源:发表于2020-05-15 10:37 被阅读0次
     /**
         * 添加淘宝商品详细
         * @return array|mixed
         * @throws \think\exception\PDOException
         */
        public function taobaoadd()
        {
    
            ignore_user_abort();
            set_time_limit(0);
            $id = trim(input('url'));
    //        $id = 542146172340;
            $url = "https://detail.m.tmall.com/item.htm?id=" . $id . "&tbpm=3";
            $response = $this->http_curl($url);
            $response = mb_convert_encoding($response, 'UTF-8', 'UTF-8,GBK,GB2312,BIG5');
            preg_match_all('/data-ks-lazyload="(http.+?)"/i', $response, $matches);
    
            $arr = array();
            foreach ($matches[1] as $k => $v) {
                $arr[] = "<p>" .
                    "<img src=" ."'".$v ."'" ."/>".
                    "</p>";
            }
            $str = implode('', $arr);//把数组元素组合为一个字符串
            session('detailed', $str);
            dump($_SESSION['detailed']);
        }
    
        /**
         *
         * @param type $url
         * @param type $type
         * @param type $arr
         * @return type
         */
        public function http_curl($url, $type = 'get', $arr = '')
        {
    
            if ($arr) {
                $o = "";
                foreach ($arr as $k => $v) {
                    $o .= "$k=" . urlencode($v) . "&";
                }
                $arr = substr($o, 0, -1);
            }
    
            $ch = curl_init();
    
            $headers = array(
                "cache-control: no-cache"
            );
    
            $user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36";
    
            curl_setopt($ch, CURLOPT_URL, $url); //设置访问的地址
            curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
    //        curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
    //        curl_setopt($ch, CURLOPT_HEADER, 1 );
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //获取的信息返回
            curl_setopt($ch, CURLOPT_REFERER, "www.baidu.com");
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    //        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
    //        curl_setopt($ch,CURLOPT_COOKIE,$cookie);
            if ($type == 'post') {
                curl_setopt($ch, CURLOPT_POST, 1);
                curl_setopt($ch, CURLOPT_POSTFIELDS, $arr);
            }
            $output = curl_exec($ch);
            if (curl_error($ch)) {
                return curl_error($ch);
            }
            return $output;
    
        }
    

    相关文章

      网友评论

          本文标题:php采集天猫商城详细

          本文链接:https://www.haomeiwen.com/subject/lbgqohtx.html