在CURL请求上返回图像

时间:2014-10-13 13:57:38

标签: php curl

我使用CURL加载网页并将其返回到我服务器上的页面。但是当页面返回时,没有显示图像,因为它们是使用href =“/ image.png”等链接的....有没有办法使用CURL将url添加到任何启动href =“/

function pull_html($url, $device)
{
    $ch = curl_init();
    if($device == 'iPhone'){
        curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (iPhone; CPU iPhone OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5376e Safari/8536.25');
    }elseif($device == 'iPad'){
        curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (iPad; CPU OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5376e Safari/8536.25');
    }
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch,CURLOPT_BINARYTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,10);
    return curl_exec($ch);
    curl_close($ch);
}

1 个答案:

答案 0 :(得分:2)

使用简单的str_replace()

return str_replace('href="/', 'href="'.$url.'/', curl_exec($ch));

http://php.net/manual/en/function.str-replace.php