简体   繁体   English

为什么file_get_contents($ url)或file_get_contents(urlencode($ url))返回http请求失败

[英]Why file_get_contents($url) or file_get_contents(urlencode($url)) returns http request fail

when i use library intervention/image in laravel 4, with piece of code $image = Image::make($url) i meet error: 当我在laravel 4中使用库干预/图像时,代码段$ image = Image :: make($ url)我遇到错误:

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 : 我已经研究了许多关于stackoverflow的解决方案,并尝试了它,例如:使用urlencode,但是我还有另一个错误:

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. 请不要建议我使用curl,因为file_get_contents()是库中的代码,我无法更改。 Thank you 谢谢

domain ticket.at-newyork.com need user-agent header in request to get content remotely. 域ticket.at-newyork.com在请求中需要用户代理标头才能远程获取内容。 have a look on below solution it works for me. 请看以下对我有用的解决方案。

$opts = array(
    'http'=>array(
        'method'=>"GET",
        'header'=>"Host: ticket.at-newyork.com\r\n" .
            "User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.80 Safari/537.36"
    )
);

$context = stream_context_create($opts);

$data = file_get_contents('http://ticket.at-newyork.com/image/tenboudai/rockfeller-top.jpg', false, $context);
file_put_contents('test.jpg', $data);

you can also use curl by setting CURLOPT_USERAGENT 您还可以通过设置CURLOPT_USERAGENT来使用CURLOPT_USERAGENT

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: 而是在param中传递url,我在控制器中使用cURL来获取二进制图像数据并将其传递给param,这是我的代码:

    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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM