当我在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
我已经研究了很多关于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
请不要建议我使用curl,因为file_get_contents()是库中的代码,我无法更改。谢谢
答案 0 :(得分:1)
domain ticket.at-newyork.com在请求中需要用户代理标头才能远程获取内容。看看下面的解决方案它对我有用。
$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);
您还可以通过设置CURLOPT_USERAGENT
答案 1 :(得分:1)
这是我的解决方案,有助于解决它。而是在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);