2014-02-27 90 views
1

我有一個遠程URL輸出該圖像 的URL是在格式這樣如何從php中獲取圖像?

http://domain.com/my_file/view/<file_id>/FULL/ 

在URL「my_file」是控制器的名字,「視圖」是函數名和另外兩個是參數

如果我在瀏覽器中點擊這個網址就說明我的圖像

我要採取像在我的項目文件夾

我與的file_get_contents嘗試,但它給我提個醒g with 404

我該如何做到這一點?

+0

給一個真實的網址也許吧? – dachi

+1

如果圖像是404,那麼它或者不存在,或者您設置的任何限制對它的訪問的子句(例如水蛭保護,基於用戶代理/引用者的限制)沒有得到滿足。這張圖片在您控制的服務器/主機上嗎? – Seidr

+0

給你的真實完整圖像網址你的網址必須是http://domain.com/my_file/view/ /FULL/image.png或必須指向圖像名稱 –

回答

0

使用捲曲此:

功能curlFile($網址) {

$ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_HEADER, 0); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 

    $data = curl_exec($ch); 
    curl_close($ch); 

    return $data; 
} 

function readurl() 
{ 
$url="http://domain.com/my_file/view/<file_id>/FULL/"; 
curlFile($url); 
} 

echo readurl(); 
2
$img=file_get_contents('http://example.com/image/test.jpg'); 
file_put_contents('/your/project/folder/imgname.jpg',$img); 

如果allow_url_fopen在php.ini文件設置爲1,這僅適用。 如果你可以改變這個值,啓用它,你就完成了。

另一個選項是CURL。檢查您的PHP配置中是否啓用了此模塊。

$ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, 'http://example.com/image/test.jpg'); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); 
    curl_setopt($ch, CURLOPT_HEADER , 0); 
    curl_setopt($ch, CURLOPT_VERBOSE, 0); 
    curl_setopt($ch, CURLOPT_TIMEOUT, 10); 
    $result = @curl_exec($ch); 
    $curl_err = curl_error($ch); 
    curl_close($ch); 
    if (empty($curl_err)) { 
    file_put_contents('/your/project/folder/imgname.jpg',$result); 
    } 

如果未啓用捲曲,你的機會是寫這樣一個簡單的HTTP客戶端:如果你想

$image = file_get_contents('http://domain.com/my_file/view/<file_id>/FULL/'); 
if($image) file_put_contents('some/folder/<file_id>'); 

$buf=''; 
    $fp = fsockopen('example.com',80); 
    fputs($fp, "GET /image/test.jpg HTTP/1.1\n"); 
    fputs($fp, "Host: example.com\n"); 
    fputs($fp, "Connection: close\n\n"); 
    while (!feof($fp)) { 
    $buf .= fgets($fp,128); 
    } 
    fclose($fp); 
    file_put_contents('/your/project/folder/imgname.jpg',$buf); 
0

,它不工作,它大概意思是:

  1. 有存取控制tha t可以防止它在遠程服務器上。在這種情況下,您必須設置適當的cookie。我建議使用curl來做到這一點。
  2. 圖像路徑錯誤;嘗試使用瀏覽器導航到http://domain.com/my_file/view/<file_id>/FULL/時查看源代碼。
  3. 你的網址中的尾部/不應該在那裏,例如http://domain.com/my_file/view/<file_id>/FULL