首页 新闻 会员 周边 捐助

php 抓取ajax返回的图片

0
悬赏园豆:20 [已关闭问题] 关闭于 2017-09-06 10:09

不知道怎么回事返回的数据为空,实在没得办法了,哪位大神解决下

<?php
/*
抓取
http://pic.hao123.com/
图片,
图片是ajax动态载入的。
*/
function replaceBadChar($fileName)
{
    // 去掉文件名中的无效字符,如 \ / : * ? " < > |
    $fileName=str_replace('\\','_',$fileName);
    $fileName=str_replace('/','_',$fileName);
    $fileName=str_replace(':','_',$fileName);
    $fileName=str_replace("*",'_',$fileName);
    $fileName=str_replace("?",'_',$fileName);
    $fileName=str_replace('"','_',$fileName);
    $fileName=str_replace('<','_',$fileName);
    $fileName=str_replace('>','_',$fileName);
    $fileName=str_replace('|','_',$fileName);
    return $fileName;
}


$dir="images/";
$startTime=microtime(true);
if(!file_exists($dir)) mkdir($dir,0777);
set_time_limit(0);
$i=1;
$j=1;
while($i<10)
{
    /*
    $url='http://pic.hao123.com/screen/'.$i.'?v='.time().'&act=type';
    $file=file_get_contents($url);
    */
    $url='https://webapi.500px.com/licensing?type=market&q=women&sort=relevance&availability=prime,subprime,editorial,subeditorial&image_size[]=1&image_size[]=2&image_size[]=32&image_size[]=31&image_size[]=33&image_size[]=34&image_size[]=35&image_size[]=36&image_size[]=2048&image_size[]=4&image_size[]=14&include_states=true&formats=jpeg,lytro&include_tags=true&exclude_nude=true&page='.$i.'&rpp=50';
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
     
    curl_setopt($ch, CURLOPT_REFERER, "https://500px.com/search?q=women&type=market&sort=relevance");   //构造来路
    
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:55.0) Gecko/20100101 Firefox/55.0");
    curl_setopt($ch,CURLOPT_HEADER,0);//不获取header信息
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  
    $out = curl_exec($ch);
    //var_dump($out);die();
     curl_close($ch);
    
    $data=json_decode($out); //array
    var_dump($data);die();
    //print_r($data[1]->picurl_orig);
     
    if($data)
    {
        foreach($data as $k=>$v)
        {
             
            print_r($v->image_url[10]);
            print_r("<br/>");
           
            $imgUrl=$v->image_url[10];
             
            //$newFile=$dir.pathinfo(replaceBadChar($imgUrl),PATHINFO_BASENAME);
             $newFile=replaceBadChar($imgUrl);
             $newFile=$dir.$newFile;
             // $contentType=$_SERVER['CONTENT_TYPE'];不能,undefined index
             $ch2=curl_init($imgUrl);
             curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);
             curl_exec($ch2);
             $contentType=curl_getinfo($ch2,CURLINFO_CONTENT_TYPE);
             //输出如:image/jpeg"
              curl_close($ch2);
             print($contentType."<br/>");
             $ext=substr($contentType,6);
             //有些图片是网址格式如 http://img.hb.aicdn.com/0c6012d12407da6b2adafc4f02779cb013a63a011dc69-x2q4Fz
             $suffix=strrchr($imgUrl,'.');
             
             print $suffix."<br/>";
             if($suffix!='.jpeg' && $suffix!='.jpg' && $suffix!='.png'  && $suffix!='.gif')
             {
                 $newFile.='.'.$ext;//添加扩展名
             }
             
             file_put_contents($newFile,file_get_contents($imgUrl));
            
            //print $i."_".$j.'_'.$imgUrl+" is ok<br/";
            $j++;
         

        }
    }
    else
    {
            echo 'img is all and usertime '.(microtime(true)-$startTime);
            die();
    }
    $i++;
     
}

php
FL陌上花开的主页 FL陌上花开 | 初学一级 | 园豆:52
提问于:2017-08-31 15:59
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册