创建 Picasa 相册并使用 PHP 和 cURL 将图像上传到其中

发布于 2024-11-30 08:31:52 字数 2568 浏览 0 评论 0原文

我发现的所有创建 Picasa 相册和上传图片的教程都使用我没有研究过的 Zend 框架。

是否可以使用 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脚本上传照片时如何添加照片许可证?

参考 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

检查 api get 的响应数据相册照片,应该有这样的内容:

       "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"
        },

All of the tutorials I've found for creating Picasa albums and uploading pictures use the Zend Framework which I haven't studied.

Is it possible to upload images and create albums using PHP and cURL?

My images are stored in the directory e:/images and the image information is stored in a MySQL table like this:

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 ;

I'm getting the Google Client authentication code using the following code:

<?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  
?> 

Can anyone give some guidance on creating an album named test and uploading the images to it?

EDIT1:

How to add photo license when I upload photos with php scripts?

reference on 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

Check the response data from api get album photo, there should be have something like:

       "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"
        },

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不回头走下去 2024-12-07 08:31:52

这是创建相册的一些代码。我们将通过 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);

Here's some code for creating the album. We'll pick up with the cURL call to authenticate.

//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);

Uploading a photo works in a similar fashion:

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

Here's functioning code for uploading an image without metadata:

$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);

And here's an example of uploading a photo with metadata (finally!):

$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);
记忆で 2024-12-07 08:31:52

扩展Mark的答案,您可以使用CURLOPT_POSTFIELDS直接从curl发送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);  

Expanding Mark's answer, you can send the XML directly from curl using CURLOPT_POSTFIELDS. However, instead of making an associative array, try passing the actual XML string like so:

$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);  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文