youtube / api-samples

Code samples for YouTube APIs, including the YouTube Data API, YouTube Analytics API, and YouTube Live Streaming API. The repo contains language-specific directories that contain the samples.
5.48k stars 2.98k forks source link

Can't download report with php #70

Open kikusempai opened 8 years ago

kikusempai commented 8 years ago

In file api-samples/php/retrieve_reports.php on lines 149-150 an error is triggered by this code:

$request = $youtubeReporting->media->download("");
$request->setUrl($reportUrl);

Method setUrl() is not defined in class GuzzleHttp\Psr7\Request.

What's more important, that Request object doesn't have such field. It has URI (GuzzleHttp\Psr7\Uri Object). So I replaced that lines with following:

$uri = new GuzzleHttp\Psr7\Uri($reportUrl);
$request = new GuzzleHttp\Psr7\Request('get', $uri);

It removes existing error. But the new one is generated:

Fatal error: Call to undefined method GuzzleHttp\Psr7\Response::getResponseBody()

Even changing $request->getResponseBody() to $response->getStream()->getContents() doesn't really help or download a proper report.

Can anyone help me on this?

Thank you in advance.

iblessedi commented 8 years ago

One more person is waiting for the solution!

TLMcode commented 8 years ago

Good luck getting any help with any of this.

liamseys commented 7 years ago

I have the same problem.

GregoirePiat commented 7 years ago

Same issue here ...

aymanbaba commented 7 years ago

Same issue here...come guys, help us out here please

CTadmin commented 7 years ago

The bug is being backed on Bountysource https://www.bountysource.com/issues/34440456-can-t-download-report-with-php

caruanas commented 7 years ago

Same here. Anyone had any luck?

0fs commented 7 years ago

It works for me:

$googleClient->setDefer(true);

$autorization =  json_decode($googleClient->getAccessToken())->token_type.' '.json_decode($googleClient->getAccessToken())->access_token;

$headers = [
    'authorization' => $autorization,
    'alt' => 'media'
];

$request = new Google_Http_Request(
    $report->downloadUrl,
    'GET',
    $headers
);

$request->enableExpectedRaw();
$response = $googleClient->execute($request);

$googleClient->setDefer(false);

instead of

 $client->setDefer(true);

// Call the YouTube Reporting API's media.download method to download a report.
$request = $youtubeReporting->media->download("");
$request->setUrl($reportUrl);
$response = $client->execute($request);

file_put_contents("reportFile", $response->getResponseBody());
$client->setDefer(false);
jpriceonline commented 7 years ago

Google_Http_Request was removed a while back right? All the way back in July 2016: https://github.com/google/google-api-php-client/blob/master/UPGRADING.md.

Just interested to know if that still works? @sflechie your solution looks the most promising, just not sure on the Google_Http_Request?

daniel-zahariev commented 7 years ago

Faced the same issue and here's how i fixed it:

function downloadReport(Google_Service_YouTubeReporting $youtubeReporting, $reportUrl, &$htmlBody) {
  $client = $youtubeReporting->getClient();
  $client->setDefer(true);

  // Setting 'alt' param here fixes the first half of the issue
  $request = $youtubeReporting->media->download("", array("alt" => "media"));
  // Correctly updating the URL fixes the second half
  $request = $request->withUri(new \GuzzleHttp\Psr7\Uri($reportUrl));
  $response = $client->execute($request);

  file_put_contents("reportFile", $response->getBody());
  $client->setDefer(false);
}

And here's my pull request: https://github.com/youtube/api-samples/pull/131

aymanbaba commented 7 years ago

This has worked like a charm. Thank You @daniel-zahariev

Now @YouTube, please update your sample code

Aswinpookkatt commented 5 years ago

Why is the issue still open?