2013-11-22 98 views
2

我使用PHP來上傳圖片到imgur這是我使用的代碼imgur:上傳圖片用PHP

$id = 'clientId'; 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, 'https://api.imgur.com/3/image.json'); 
    curl_setopt($ch, CURLOPT_POST, TRUE); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Client-ID ' . $id)); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, array('image' => base64_encode($image))); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
    $response = curl_exec($ch); 
    $response = json_decode($response); 
    curl_close ($ch); 

的代碼工作,但我想要的圖片被上傳到我的帳戶專輯如果有人知道如何做到這一點,另外我想了解爲什麼我必須設置curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);的代碼工作,我沒有訪問manual,但如果任何人都可以給我一些背景知道如何以及爲什麼我會非常感激,謝謝大家,祝你有美好的一天。

+1

你看過他們的[API Documentation](http://api.imgur.com/endpoints/album)嗎?這似乎是有據可查的。 – Latheesan

+0

我會來看看,但是請你解釋一下關於CURLOPT_SSL_VERIFYPEER。 –

回答

1

您可以與您的要求發送這些參數:

image required A binary file, base64 data, or a URL for an image 
album optional The id of the album you want to add the image to. For anonymous albums, {album} should be the deletehash that is returned at creation. 
type optional The type of the file that's being sent; file, base64 or URL 
name optional The name of the file, this is automatically detected if uploading a file with a POST and multipart/form-data 
title optional The title of the image. 
description optional The description of the image. 

那麼,這個怎麼樣:

curl_setopt($ch, CURLOPT_POSTFIELDS, array('image' => base64_encode($image), 'album' => 1234)); 
+0

嘿,我確實嘗試了'album'=>我的專輯代碼,但是我收到一個錯誤消息:我不是該專輯的所有者,您知道爲什麼嗎? –

+0

您是否在登錄時發佈了圖片? https://api.imgur.com/oauth2#authorization –

+0

不是我沒有登錄我只提供客戶端ID,你能給我一個例子,請... –

1

通過設置CURLOPT_SSL_VERIFYPEER爲假,也不會驗證上的SSL證書的站點。通常,證書設置爲xyz.com域,而不是所有xyz的子域(由於這些證書不太常見而更昂貴)。

由於它在您將其設置爲false時起作用,這意味着它們的ssl證書未針對特定域路徑創建。

通過設置爲true,它將嘗試驗證證書,並在該特定域路徑不存在證書時停止連接。希望這有幫助。

+0

謝謝你的幫助。 –