Create a Picasa album and upload images to it with PHP and cURL

前端 未结 2 1893
忘掉有多难
忘掉有多难 2020-12-09 11:45

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 a

2条回答
  •  佛祖请我去吃肉
    2020-12-09 12:28

    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 = "
                  Test album from PHP
                  This is a test album
                  Louisville
                  public
                  1152255600000
                  
                ";
    
    //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 = '
                  plz-to-love-realcat.jpg
                  Real cat wants attention too.
                  
                ';
    
    $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);
    

提交回复
热议问题