Ad

Why File_get_contents($url) Or File_get_contents(urlencode($url)) Returns Http Request Fail

- 1 answer

when i use library intervention/image in laravel 4, with piece of code $image = Image::make($url) i meet error:

file_get_contents(http://ticket.at-newyork.com/image/tenboudai/rockfeller-top.jpg): failed to open stream: HTTP request failed! HTTP/1.1 410 Gone

I have researched many solution on stackoverflow and try it ex : use urlencode but i have another error :

file_get_contents(http%3A%2F%2Fticket.at-newyork.com%2Fimage%2Ftenboudai%2Frockfeller-top.jpg): failed to open stream: No such file or directory

Please don't suggest me use curl, because file_get_contents() is code in library and i can't change. Thank you

Ad

Answer

This is my solution that help resolve it. Instead pass url in param, i use cURL in controller to get binary image data and pass it to param, this is my code:

    curl_handle = curl_init();
    curl_setopt($curl_handle, CURLOPT_URL,$url);
    curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
    curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl_handle, CURLOPT_USERAGENT, 'Vivo app');
    $query = curl_exec($curl_handle);
    curl_close($curl_handle);

    try {
        $image = Image::make($query)->save($destinationPath . $img_name);
Ad
source: stackoverflow.com
Ad