2011-08-20 2 views
10

Picasa 앨범을 만들고 사진을 업로드 할 때 찾은 모든 자습서는 내가 공부하지 않은 Zend Framework를 사용합니다.Picasa 앨범 만들기 및 PHP 및 cURL을 사용하여 이미지 업로드하기

PHP 및 cURL을 사용하여 이미지를 업로드하고 앨범을 만들 수 있습니까?

내 이미지는 디렉토리 e:/images에 저장되어있는 이미지 정보는 다음과 같이 MySQL의 테이블에 저장됩니다

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO"; 
CREATE TABLE IF NOT EXISTS `picasaimage` (
    `id` bigint(1) unsigned NOT NULL AUTO_INCREMENT, 
    `title` varchar(255) COLLATE utf8_unicode_ci NOT NULL, 
    `content` varchar(255) COLLATE utf8_unicode_ci NOT NULL, 
    `tags` varchar(255) COLLATE utf8_unicode_ci NOT NULL, 
    `license` varchar(50) COLLATE utf8_unicode_ci NOT NULL, 
    `image_path` varchar(150) COLLATE utf8_unicode_ci NOT NULL, 
    `width` int(4) COLLATE utf8_unicode_ci NOT NULL, 
    `height` int(4) COLLATE utf8_unicode_ci NOT NULL, 
    PRIMARY KEY (`id`), 
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=0 ; 

나는 다음과 같은 코드를 사용하여 Google 클라이언트 인증 코드를 받고 있어요 :

<?php 
$ch = curl_init(); 

curl_setopt($ch, CURLOPT_URL, "https://www.google.com/accounts/ClientLogin"); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 

$data = array('accountType' => 'GOOGLE', 
'Email' => '[email protected]', 
'Passwd' => 'yourpassword', 
'source'=>'PHI-cUrl-Example', 
'service'=>'lh2'); 

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); 
curl_setopt($ch, CURLOPT_POST, true); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 

$hasil = curl_exec($ch); 

echo $hasil; 
//SID=DQA...oUE 
//LSID=DQA...bbo 
//Auth=DQA...Sxq 
?> 

test 앨범을 만들고 이미지를 업로드하는 방법에 대한 지침이 있습니까?

EDIT1 : 나는 PHP 스크립트와 사진을 업로드 할 때

어떻게 사진을 라이센스를 추가? API의 응답 데이터가 앨범 사진을 얻을 http://commons.wikimedia.org/wiki/Commons:Picasa_Web_Albums_files

Creative Commons Attribution 3.0 Unported (CC-BY) 
Creative Commons Attribution-Share Alike 3.0 Unported 
Unlicensed 
Creative Commons Attribution-Noncommercial 3.0 Unported 
Creative Commons Attribution-No Derivative Works 3.0 Unported 
Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 Unported 

확인에

참조, 같은이가 있어야한다 :

 "gphoto$license":{ 
      "$t":"ATTRIBUTION_NON_COMMERCIAL_NO_DERIVATIVES", 
      "id":3, 
      "name":"Attribution-Noncommercial-No Derivative", 
      "url":"http://creativecommons.org/licenses/by-nc-nd/3.0" 
     }, 

답변

4

을 다음 앨범을 만들기위한 몇 가지 코드입니다. 인증을위한 cURL 호출을 받아 들일 것입니다. 비슷한 방식으로 사진을 작품을 업로드

//This is the cURL call to authenticate. We'll splitting out the return values 
//to more easily get the auth code. 
$ret = explode("\n",curl_exec($ch)); 

//Save all of the return values to an array so we can get them more easily later 
$gvals = array(); 
foreach($ret as $item) { 
    $flds = explode("=", $item); 

    if(count($flds) > 1) { 
     $gvals[$flds[0]] = $flds[1]; 
    } 
} 

//This is the authentication header we'll need to pass with each successive call 
$authHeader = 'Authorization: GoogleLogin auth="' . $gvals['Auth'] . '"'; 
$userId = "THE PICASA USER ID GOES HERE"; 
$feedUrl = "https://picasaweb.google.com/data/feed/api/user/$userId"; 

//This is the XML for creating a new album. 
$rawXml = "<entry xmlns='http://www.w3.org/2005/Atom' 
       xmlns:media='http://search.yahoo.com/mrss/' 
       xmlns:gphoto='http://schemas.google.com/photos/2007'> 
       <title type='text'>Test album from PHP</title> 
       <summary type='text'>This is a test album</summary> 
       <gphoto:location>Louisville</gphoto:location> 
       <gphoto:access>public</gphoto:access> 
       <gphoto:timestamp>1152255600000</gphoto:timestamp> 
       <category scheme='http://schemas.google.com/g/2005#kind' 
       term='http://schemas.google.com/photos/2007#album'></category> 
      </entry>"; 

//Setup our cURL options 
//Notice the last one where we pass in the authentication header 
$options = array(
      CURLOPT_URL=> $feedUrl, 
      CURLOPT_SSL_VERIFYPEER=> false, 
      CURLOPT_POST=> true, 
      CURLOPT_RETURNTRANSFER=> true, 
      CURLOPT_HEADER=> true, 
      CURLOPT_FOLLOWLOCATION=> true, 
      CURLOPT_POSTFIELDS=> $rawXml, 
      CURLOPT_HTTPHEADER=> array('GData-Version: 2', $authHeader, 'Content-Type: application/atom+xml') 
     ); 
curl_setopt_array($ch, $options); 

//This call will create the Picasa album. 
//The return value is XML with a bunch of information about the newly created album. 
$ret = curl_exec($ch); 

: 여기

http://code.google.com/apis/picasaweb/docs/2.0/developers_guide_protocol.html#PostPhotos

는 메타 데이터없이 이미지 을 업로드 코드를 작동하는 것 :

$userId = "USER ID GOES HERE"; 
$albumId = "ALBUM ID GOES HERE"; 
$albumUrl = "https://picasaweb.google.com/data/feed/api/user/$userId/albumid/$albumId"; 
$imgName = $_SERVER['DOCUMENT_ROOT'] . '/picasa/cute_baby_kitten.jpg'; 

//Get the binary image data 
$fileSize = filesize($imgName); 
$fh = fopen($imgName, 'rb'); 
$imgData = fread($fh, $fileSize); 
fclose($fh); 

$header = array('GData-Version: 2', $authHeader, 'Content-Type: image/jpeg', 'Content-Length: ' . $fileSize, 'Slug: cute_baby_kitten.jpg'); 
$data = $imgData; //Make sure the image data is NOT Base64 encoded otherwise the upload will fail with a "Not an image" error 

$ret = ""; 
$ch = curl_init($albumUrl); 
$options = array(
     CURLOPT_SSL_VERIFYPEER=> false, 
     CURLOPT_POST=> true, 
     CURLOPT_RETURNTRANSFER=> true, 
     CURLOPT_HEADER=> true, 
     CURLOPT_FOLLOWLOCATION=> true, 
     CURLOPT_POSTFIELDS=> $data, 
     CURLOPT_HTTPHEADER=> $header 
    ); 
curl_setopt_array($ch, $options); 
$ret = curl_exec($ch); 
curl_close($ch); 

그리고 여기가 메타 데이터와 사진 을 업로드의 예 (드디어!) :

$albumUrl = "https://picasaweb.google.com/data/feed/api/user/$userId/albumid/$albumId"; 
$imgName = $_SERVER['DOCUMENT_ROOT'] . '/picasa/cute_baby_kitten.jpg'; 

$rawImgXml = '<entry xmlns="http://www.w3.org/2005/Atom"> 
       <title>plz-to-love-realcat.jpg</title> 
       <summary>Real cat wants attention too.</summary> 
       <category scheme="http://schemas.google.com/g/2005#kind" 
       term="http://schemas.google.com/photos/2007#photo"/> 
      </entry>'; 

$fileSize = filesize($imgName); 
$fh = fopen($imgName, 'rb'); 
$imgData = fread($fh, $fileSize); 
fclose($fh); 

$dataLength = strlen($rawImgXml) + $fileSize; 
$data = ""; 
$data .= "\nMedia multipart posting\n"; 
$data .= "--P4CpLdIHZpYqNn7\n"; 
$data .= "Content-Type: application/atom+xml\n\n"; 
$data .= $rawImgXml . "\n"; 
$data .= "--P4CpLdIHZpYqNn7\n"; 
$data .= "Content-Type: image/jpeg\n\n"; 
$data .= $imgData . "\n"; 
$data .= "--P4CpLdIHZpYqNn7--"; 

$header = array('GData-Version: 2', $authHeader, 'Content-Type: multipart/related; boundary=P4CpLdIHZpYqNn7;', 'Content-Length: ' . strlen($data), 'MIME-version: 1.0'); 

$ret = ""; 
$ch = curl_init($albumUrl); 
$options = array(
     CURLOPT_SSL_VERIFYPEER=> false, 
     CURLOPT_POST=> true, 
     CURLOPT_RETURNTRANSFER=> true, 
     CURLOPT_HEADER=> true, 
     CURLOPT_FOLLOWLOCATION=> true, 
     CURLOPT_POSTFIELDS=> $data, 
     CURLOPT_HTTPHEADER=> $header 
    ); 
curl_setopt_array($ch, $options); 
$ret = curl_exec($ch); 
curl_close($ch); 
+0

안녕하세요. 또 다른 질문, 내가 업로드 할 때 사진 라이센스를 추가하는 방법? 감사. –

+0

앨범 제작 코드가 업데이트되었습니다. 내가 알아낼 수 있다면 사진 업로드 코드를 게시 할 것입니다. –

+0

좋아요, 마침내 이미지를 업로드하기위한 작업 코드가 있습니다. 그래도 여전히 메타 데이터로 이미지를 업로드하는 데 어려움을 겪고 있습니다. –

1

마크의 답변을 확장, 당신은 CURLOPT_POSTFIELDS를 사용하여 컬에서 직접 XML을 보낼 수 있습니다. 그러나 연관 배열을 만드는 대신 실제 XML 문자열을 다음과 같이 전달해보십시오.

$data= "<entry xmlns='http://www.w3.org/2005/Atom' 
    xmlns:media='http://search.yahoo.com/mrss/' 
    xmlns:gphoto='http://schemas.google.com/photos/2007'> 
    <title type='text'>Trip To Italy</title> 
    <summary type='text'>This was the recent trip I took to Italy.</summary> 
    <gphoto:location>Italy</gphoto:location> 
    <gphoto:access>public</gphoto:access> 
    <gphoto:timestamp>1152255600000</gphoto:timestamp> 
    <media:group> 
    <media:keywords>italy, vacation</media:keywords> 
    </media:group> 
    <category scheme='http://schemas.google.com/g/2005#kind' 
    term='http://schemas.google.com/photos/2007#album'></category> 
</entry>"; 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); 
curl_setopt($ch, CURLOPT_POST, true); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 

$hasil = curl_exec($ch); 
관련 문제