duanfei8897 2017-02-08 19:04
浏览 56
已采纳

cURL从链接获取图像

I have a script in php how download images from a partner website.

The script looks like

function getimg($url) {
    $headers[] = 'Accept: image/gif, image/x-bitmap, image/jpeg, image/pjpeg';
    $headers[] = 'Connection: Keep-Alive';
    $headers[] = 'Content-type: application/x-www-form-urlencoded;charset=UTF-8';
    $user_agent = 'php';
    $process = curl_init($url);
    curl_setopt($process, CURLOPT_USERPWD, "username:password");
    curl_setopt($process, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    curl_setopt($process, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($process, CURLOPT_HEADER, 0);
    curl_setopt($process, CURLOPT_USERAGENT, $user_agent);
    curl_setopt($process, CURLOPT_TIMEOUT, 30);
    curl_setopt($process, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($process, CURLOPT_FOLLOWLOCATION, 1);
    $return = curl_exec($process);
    curl_close($process);
    return $return;
}

The problem is because partner was put a password on his website. the website url is www.importatorarticolecopii.ro/feeds/general_feed2.php

I put the user and password in curl but not work...

Need some help

</div>
  • 写回答

1条回答 默认 最新

  • douhan8009 2017-02-08 20:54
    关注

    Your code is fine, the problem is from your given url Increase your timeout as the url given above is too slow and try to don't exceed the maximum execution time of your server.

    curl_setopt($process, CURLOPT_TIMEOUT, 60);
    

    and use a user agent for example :

    $user_agent = 'Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7.12) Gecko/2php0050915 Firefox/1.0.7)';
    

    and in future try to catch your errors to check what's your issue

    if(curl_error($process))
    {
    echo 'error:' . curl_error($process);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)