php爬取第三方接口get请求

2021/7/17 11:35:05

本文主要是介绍php爬取第三方接口get请求,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

爬数组

        $str='https://www.changjingdian.com/api/dynamic/DescribeDynamicForBusinessByPage?appSecret=saishangcff8d98e662d47ab98d23cb2041ee2d9';
        $data=file_get_contents($str);
        $data=json_decode($data,true);
      dd($data);
         //  $data=[];
        $en=[];
                foreach($data['data']['list']as $k =>$val){
         //  $en[$k]['id']=isset($val['id']) ?$val['id'] : '';
          // $en[$k]['dynamicId']=isset($val['dynamicId']) ?$val['dynamicId'] : '';
            $en[$k]['coverImage']=isset($val['coverImage']) ?$val['coverImage'] : '';
            $en[$k]['coverImageWidth']=isset($val['coverImageWidth']) ?$val['coverImageWidth'] : '';
            $en[$k]['coverImageHeight']=isset($val['coverImageHeight']) ?$val['coverImageHeight'] : '';
                    $en[$k]['type']=isset($val['type']) ?$val['type'] : 0;
                    $en[$k]['creatorUserName']=isset($val['creatorUserName']) ?$val['creatorUserName'] : '';
                    $en[$k]['creatorPhone']=isset($val['creatorPhone']) ?$val['creatorPhone'] : '';
                    $en[$k]['storeId']=isset($val['storeId']) ?$val['storeId'] : '';
                    $en[$k]['storeUserId']=isset($val['storeUserId']) ?$val['storeUserId'] : '';
                    $en[$k]['createTime']=isset($val['createTime']) ?$val['createTime'] : '';
        }
//        User::query()->insert($en);
       DB::table('zz')->insert($en);
        //dd($en);

爬对象 就是单值 如果爬行的单值是数组 记得用json_encode转换

  $str='https://www.changjingdian.com/api/dynamic/DescribeDynamicInfo?appSecret=saishangcff8d98e662d47ab98d23cb2041ee2d9&dynamicId=22360';
        $data=file_get_contents($str);

        $data=json_decode($data,true);

       // dd($data['data']['dynamicForBusinessView']);
          //$data=[];
                $s=$data['data']['dynamicForBusinessView'];
               //dd($s);
        $en=[];

            // $en[$k]['id']=isset($val['id']) ?$val['id'] : '';
             $en['dynamicId']=isset($val['dynamicId']) ?$s['dynamicId'] : 0;
            $en['coverImage']=isset($s['coverImage']) ?$s['coverImage'] : '';
            $en['coverImageWidth']=isset($s['coverImageWidth']) ?$s['coverImageWidth'] : '';
            $en['coverImageHeight']=isset($s['coverImageHeight']) ?$s['coverImageHeight'] : '';
            $en['resourceUrls']=isset($s['resourceUrls']) ?json_encode($s['resourceUrls']): '';
            $en['type']=isset($s['type']) ?$s['type'] : 0;
            $en['creatorUserName']=isset($s['creatorUserName']) ?$s['creatorUserName'] : '';
            $en['creatorPhone']=isset($s['creatorPhone']) ?$s['creatorPhone'] : '';
            $en['storeId']=isset($s['storeId']) ?$s['storeId'] : '';
            $en['storeUserId']=isset($s['storeUserId']) ?$s['storeUserId'] : '';
            $en['createTime']=isset($s['createTime']) ?$s['createTime'] : '';

//        User::query()->insert($en);

        DB::table('bb')->insert($en);
        dd($en);


这篇关于php爬取第三方接口get请求的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程