Sounds definitely like an Auth issue, though I'm not surewhy you're having
problems.
Here's my full test page:
<?php
require_once 'Zend/Loader.php';
Zend_Loader::loadClass('Zend_Gdata');
Zend_Loader::loadClass('Zend_Gdata_AuthSub');
Zend_Loader::loadClass('Zend_Gdata_Docs');
try {
$docs = setupDocsClient(@$_GET['token']);
if ($docs) {
$feed = retrieveWPDocs($docs);
printDocumentsFeed($feed);
// export first doc
$contentLink = $feed->entries[0]->content->getSrc();
$fileContents = download($docs, $contentLink, 'txt');
echo 'Contents of document "' . $feed->entries[0]->title . '":<hr>';
echo "<pre>$fileContents</pre>";
} else {
exit(); // Just display the AuthSub link in setupDocsClient()
}
} catch (Zend_Gdata_App_AuthException $e) {
echo "Error: Unable to authenticate. Please check your token.\n";
exit(1);
}
function download($client, $url, $format=null) {
$token = $client->getHttpClient()->getAuthSubToken();
$opts = array(
'http' => array(
'method' => 'GET',
'header' => "GData-Version: 2.0\r\n".
"Authorization: AuthSub token=\"$token\"\r\n"
)
);
$context = stream_context_create($opts);
if ($url != null) {
$url = $url . "&exportFormat=$format";
}
return file_get_contents($url, false, $context);
}
function setupDocsClient($singleUseToken = null) {
$docsClient = null;
// Fetch a new AuthSub token?
if (!$singleUseToken) {
$next = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
$scope = 'http://docs.google.com/feeds';
$secure = 0;
$session = 1;
$permission = 1; // 1 - allows posting notices && allows reading
profile data
$authSubURL = Zend_Gdata_AuthSub::getAuthSubTokenUri($next, $scope,
$secure, $session);
echo '<a href="' . $authSubURL . '">Link your Google Docs Account</a>';
} else {
$httpClient = new Zend_Gdata_HttpClient();
$sessionToken =
Zend_Gdata_AuthSub::getAuthSubSessionToken(trim($singleUseToken),
$httpClient);
$httpClient->setAuthSubToken($sessionToken);
$docsClient = new Zend_Gdata_Docs($httpClient);
}
return $docsClient;
}
function printDocumentsFeed($feed, $html=true) {
if ($html) {echo "<ol>\n";}
// Iterate over the document entries in the feed
// and display each document's title.
foreach ($feed->entries as $entry) {
if ($html) {
// Find the URL of the HTML view of the document.
$alternateLink = '';
foreach ($entry->link as $link) {
if ($link->getRel() === 'alternate') {
$alternateLink = $link->getHref();
}
}
// Make the title link to the document on docs.google.com.
echo "<li><a href=\"$alternateLink\">$entry->title</a></li>\n";
} else {
echo "$entry->title\n";
}
}
if ($html) {echo "</ol>\n";}
}
function retrieveWPDocs($client, $html=true)
{
return $client->getDocumentListFeed(
'http://docs.google.com/feeds/documents/private/full/-/document');
}
?>
BTW, The reason you can cut and paste
the URL into a browser is because you have a validGoogle Docs cookie set.
If you tried the same after clearing
cookies things would turn out much different :)
Eric
On Wed, Jul 22, 2009 at 5:58 PM, faninator <[email protected]> wrote:
>
> Hi Eric,
>
> Thanks for the reply. I'm not trying to download a spreadsheet. I also
> changed the feed URI to the correct one. However when I echo out the
> content, I still get the login page? This feels like some
> authentication issue. I copied your code verbatim, and tried many
> other tricks to get the file to download directly to the server using
> file_get_contents() followed by file_put_contents(). However, I keep
> getting a file containing the login page rather than the file I want.
> I can correctly retrieve everything else about the file, such as the
> name of the file, the link etc.
>
> Another observation is that when I copy and paste the file url I get
> from the response header and put that directly into browser address
> bar, I get the prompt to download the file without the login page.
> However, when I try to file_get_contents from that same url, I get the
> login page again. Could it be something wrong with the $opts array or
> session token?
>
> Thanks,
> Fan
>
> On Jul 22, 2:20 pm, "Eric (Google)" <[email protected]> wrote:
> > Are you trying to download a spreadsheet? If so,
> > be sure to also obtain an AuthSub token good forhttp://
> spreadsheets.google.com/feeds.
> >
> > Also, there's a mistake in that snippet I posted.
> > The feed URI should be
> > 'http://docs.google.com/feeds/documents/private/full/-/document'
> >
> > Eric
> >
> > On Jul 22, 6:00 am, faninator <[email protected]> wrote:
> >
> > > Hi Eric,
> >
> > > I tried your method of using file_get_contents with the example code
> > > that you posted but I was unable to download the file to the server.
> > > If I echo the result from the download function, I get a broken google
> > > login page and then a prompt for download. However, when I'm trying to
> > > write the file content to a new file on my server (which is my
> > > ultimate goal here), it writes the google login page into the file. Do
> > > you know what could be causing the problem?
> >
> > > Thanks,
> > > Fan
> >
> > > On Jul 15, 4:32 pm, "Eric (Google)" <[email protected]> wrote:
> >
> > > > Posted a tip on this:
> http://gdatatips.blogspot.com/2009/07/download-google-doc-using-php-l...
> >
> > > > On Jul 15, 1:13 pm, "Eric (Google)" <[email protected]> wrote:
> >
> > > > > Hi,
> >
> > > > > You should be using the <entry>'s content src link
> > > > > and not the alternate (/View?docID=...) link that
> > > > > points to Google Docs. Having said that,
> > > > > you'll need to write a download method yourself
> > > > > b/c the Zend_Gdata_Docs component doesn't contain
> > > > > this latest API feature.
> >
> > > > > Here's how you _could_ use file_get_contents() to
> > > > > export a doc as a .txt file. Note: to export
> > > > > spreadsheets, you'll need a valid AuthSub session
> > > > > token for scope=http://spreadsheets.google.com/feeds/
> > > > > as well.
> >
> > > > > $feed = $gdClient->getDocumentListFeed(
> > > > > 'http://docs.google.com/feeds/documents/private/full/-document'
> );
> >
> > > > > // download first entry
> > > > > $contentLink = $feed->entries[0]->content->getSrc();
> > > > > $fileContents = download($gdClient, $contentLink, 'txt');
> >
> > > > > echo 'Contents of document "' . $feed->entries[0]->title .
> '":<hr>';
> > > > > echo "<pre>$fileContents</pre>";
> >
> > > > > function download($client, $url, $format=null) {
> > > > > $token = $client->getHttpClient()->getAuthSubToken();
> > > > > $opts = array(
> > > > > 'http' => array(
> > > > > 'method' => "GET",
> > > > > 'header' => "GData-Version: 2.0\r\n".
> > > > > "Authorization: AuthSub token=\"$token\"\r\n"
> > > > > )
> > > > > );
> > > > > $context = stream_context_create($opts);
> >
> > > > > if ($url != null) {
> > > > > $url = $url . "&exportFormat=$format";
> > > > > }
> >
> > > > > return file_get_contents($url, false, $context);
> >
> > > > > }
> >
> > > > > Eric
> >
> > > > > On Jul 14, 7:56 pm, infinitas <[email protected]> wrote:
> >
> > > > > > $service = Zend_Gdata_Docs::AUTH_SERVICE_NAME;
> > > > > > $httpClient = Zend_Gdata_ClientLogin::getHttpClient($user, $pass,
> > > > > > $service);
> > > > > > $gdClient = new Zend_Gdata_Docs($httpClient);
> > > > > > echo file_get_contents("http://docs.google.com/View?docID={$id}
> > > > > > &revision=_latest&hgd=1");
> >
> > > > > > More or less, this is what I have *working* which raises three
> > > > > > questions.
> >
> > > > > > 1. Is fopen/curl/file_get_contents the accepted method of getting
> the
> > > > > > content or is there an API call I should be using?
> > > > > > 2. Does the document HAVE to be "published" for it to work?
> > > > > > 3. Is it necessary to have the "Google Docs -- Web word
> processing,
> > > > > > presentations and spreadsheets." in the footer? (I am assuming
> number
> > > > > > one is "no" and the correct method would probably have an option
> for
> > > > > > this).
> >
> > > > > > On Jul 14, 12:11 am, Eric Bidelman <[email protected]> wrote:
> >
> > > > > > > What have you tried?
> >
> > > > > > > On Mon, Jul 13, 2009 at 7:53 PM, infinitas <
> [email protected]> wrote:
> >
> > > > > > > > I am using the PHP Zend_Gdata API. I have successfully been
> able to
> > > > > > > > retrieve a list of my documents and search through my
> documents, but I
> > > > > > > > have not been able to retrieve the actual content inside a
> document.
> > > > > > > > Could someone please point me in the right direction?
> >
> > > > > > > > Thanks
> >
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Google Docs Data APIs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/Google-Docs-Data-APIs?hl=en
-~----------~----~----~----~------~----~------~--~---