我想用 php 檢索遠程托管的影像。影像存在,我可以使用瀏覽器訪問它。然而 curl_exec() 的結果是空的, curl_error() 說:
無法連接到 img107.xooimage.com 埠 80:連接被拒絕
這是我的代碼:
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, 'http://img107.xooimage.com/files/5/0/b/ss-2014-06-15-at-12.58.47--46324f5.png');
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$image = curl_exec($ch);
if( empty($image) ){
echo("Impossible to retrieve the file !<br>
error: ".curl_error($ch)."<br>");
}
如果我可以用瀏覽器打開圖片,那為什么我使用curl時連接被拒絕?
備注:它適用于我自己域中的影像,但不適用于示例中的外部影像。
uj5u.com熱心網友回復:
你需要在閱讀之前關閉卷曲
$ch = curl_init('http://img107.xooimage.com/files/5/0/b/ss-2014-06-15-at-12.58.47--46324f5.png');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FAILONERROR,true);
$image = curl_exec($ch);
if (curl_errno($ch)) {
print_r(curl_error($ch));
die;
}
curl_close($ch);
return $image;
uj5u.com熱心網友回復:
經過測驗,以下內容確實將給定的影像image.png沿腳本保存到檔案中,并且是可讀的。是缺少什么嗎?
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, 'http://img107.xooimage.com/files/5/0/b/ss-2014-06-15-at-12.58.47--46324f5.png');
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$image = curl_exec($ch);
var_dump($image); // Binary content
$save = fopen('image.png', 'w');
fwrite($save, $image);
fclose($save);
curl_close($ch);
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/378707.html
