2013-05-05 34 views
2

我为Google Glass开发了一个Mirror API应用程序,并且我陷入了一个非常基础的问题。我想保存时间线项目中的图像。如何从时间线项目下载图像附件?

require_once 'config.php'; 
require_once 'mirror-client.php'; 
require_once 'google-api-php-client/src/Google_Client.php'; 
require_once 'google-api-php-client/src/contrib/Google_MirrorService.php'; 
require_once 'util.php'; 
require 'facebook-php-sdk/src/facebook.php'; 

if($_SERVER['REQUEST_METHOD'] != "POST") { 
    http_send_status(400); 
    exit(); 
} 

// Parse the request body 
$body = http_get_request_body(); 
$request = json_decode($body, true); 


// A notification has come in. If there's an attached photo, bounce it back 
// to the user 
$user_id = $request['userToken']; 
$access_token = get_credentials($user_id); 

$client = get_google_api_client(); 
$client->setAccessToken($access_token); 

// A glass service for interacting with the Mirror API 
$mirror_service = new Google_MirrorService($client); 

//Save image to file 
$itemId = $request['itemId']; 
$timeLineItem = $mirror_service->timeline->get($itemId); 
$request = new Google_HttpRequest($timeLineItem['attachments'][0]['contentUrl'], 'GET',   null, null); 
$httpRequest = Google_Client::$io->authenticatedRequest($request); 
if ($httpRequest->getResponseHttpCode() == 200) { 
    $image = $httpRequest->getResponseBody(); 
    imagejpeg($image, 'test.jpg'); 
    } else { 
    // An error occurred. 
    die('This sucks! '. $httpRequest->getResponseBody()); 
    } 

我不断收到此错误: PHP的警告:imagejpeg()预计参数1是资源

我新的PHP,所以我很害怕,我甚至不是在正确的轨道。我究竟做错了什么?

回答

4

imagejpeg的第一个参数应符合 'imagecreatetruecolor()' http://www.php.net/manual/en/function.imagecreatetruecolor.php

创建资源当$ httpRequest-> getResponseBody()返回图像的二进制内容,你可以用它保存:file_put_contents('test.jpg', $httpRequest->getResponseBody());

当您的数据使用MIME base64进行编码时: file_put_contents('test.jpg', base64_decode($httpRequest->getResponseBody()));

+0

非常感谢! – theJosh 2013-05-06 04:42:28