duanqian2278 2015-12-31 17:13
浏览 221
已采纳

CURL下载图片php无法正常工作

I'm trying to download a dynamic generated image to my server using curl and php and for some reason I keep failing Can someone help out... Below is my code

function download_image($image_url){
    $ch = curl_init($image_url);
    // curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // enable if you want
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 1000);      // some large value to allow curl to run for a long time
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0');
    curl_setopt($ch, CURLOPT_WRITEFUNCTION, "curl_callback");
    curl_setopt($ch, CURLOPT_VERBOSE, true);   // Enable this line to see debug prints
    curl_exec($ch);

    curl_close($ch);                              // closing curl handle
}

/** callback function for curl */
function curl_callback($ch, $bytes){
    global $fp;
    $len = fwrite($fp, $bytes);
    // if you want, you can use any progress printing here
    return $len;
}

$image_file = "ram.png";
$fp = fopen ($image_file, 'w+');              // open file handle
download_image("http://chart.apis.google.com/chart?chl=30%&chs=300x120&cht=gm&chco=77AB10,FFFF00|FF0000&chd=t:30&chf=bg,s,232526");
fclose($fp);    
  • 写回答

3条回答 默认 最新

  • dsg7513 2015-12-31 17:34
    关注

    Maybe for this url using htmlspecialchars_decode will help when using the & in the url.

    $url = htmlspecialchars_decode("http://chart.apis.google.com/chart?chl=30%&chs=300x120&cht=gm&chco=77AB10,FFFF00|FF0000&chd=t:30&chf=bg,s,232526");
    download_image($url);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料