Hello,
I am trying to use the Google API client to make a dashboard with relevant information. However, when I follow the steps on the hello analytics tutorial, and try to connect, I get the following error:
Warning: count(): Parameter must be an array or an object that implements Countable in C:\xampp\htdocs\google\vendor\guzzlehttp\guzzle\src\Handler\CurlFactory.php on line 67
How would I go about solving this?
Thanks.
This warning has been cropping up when an older version of Guzzle is used with PHP 7.2. If upgrading Guzzle doesn't work please include a full stack trace as well as version of PHP and Guzzle.
what is a full stack trace?
my php version is 7.2.2
I am also pretty sure guzzle is version 6, since
when I update guzzle via "composer update guzzlehttp/guzzle" it says
"Updating dependencies (including require-dev)
Nothing to install or update
Generating autoload files"
So.. I am trying to update Guzzle to 6.0.3, but this doesn't work, since it says guzzlehttp/guzzle 6.3.0 requires guzzlehttp/psr7^1.4
but if I try to install psr7[1.4] it won't let me, since it says google/auth v0.8 requires guzzlehttp/psr7 1.2. but these conflict with your requirements or minimum-stability.
So is there any work-around?
One work around is to copy your composer.json to a new directory and run composer install and then to use that vendor directory to replace your old one. Another is to use a packaged release provided by us at https://github.com/google/google-api-php-client/releases
Thank you. This indeed solved the problems.
This solution didn't work form me...
The problem is in PHP 7.2 the parameter for count() can't be NULL. The warning gets displayed when $this->handles equals NULL. Just replace line 67 in CurlFactory.php with the following:
if (($this->handles ? count($this->handles) : 0) >= $this->maxHandles) {
The problem is in PHP 7.2 the parameter for count() can't be NULL. The warning gets displayed when $this->handles equals NULL. Just replace line 67 in CurlFactory.php with the following:
if (($this->handles ? count($this->handles) : 0) >= $this->maxHandles) {
it worked thanks @vinnoangel
The problem is in PHP 7.2 the parameter for count() can't be NULL. The warning gets displayed when $this->handles equals NULL. Just replace line 67 in CurlFactory.php with the following:
if (count( (array) $this->handles) >= $this->maxHandles) {
The problem is in PHP 7.2 the parameter for count() can't be NULL. The warning gets displayed when $this->handles equals NULL. Just replace line 67 in CurlFactory.php with the following:
if (($this->handles ? count($this->handles) : 0) >= $this->maxHandles) {
thanks a lot! @vinnoangel
@jdpedrie Hi John, I see that you've made some related changes recently, can we re-open this please for tracking purposes? Asking folks to manually change this project's dependencies isn't really a solution :)
Hi @beverloo
Could you give me more information on what you'd like me to track? I'd be happy to reopen the issue if there's a need, but beyond potentially requiring guzzle 6.3+, I'm not sure if there's any action I can take.
The correct solution would be to upgrade to guzzle 6.3 or above. I'm not sure why anyone would be pinning to a version of guzzle 6 below that, but I think the only way this error would occur would be if for some reason you were stuck with an older version of guzzle for a reason unrelated to this library.
Perhaps we can update the minimum version for 6.x to 6.3.0. I'll need to discuss the implications of us doing that internally, but there's certainly nothing preventing you from requiring a higher version of guzzle within the 6.x series. The downloadable archives provide (for 2.4.0) guzzle 6.3.3.
Guzzle 6.3 is indeed the requirement for PHP 7.4 compatibility, so updating the minimum version certainly seems like the correct solution to me as well. I'm not familiar with the impact of that on the rest of the project to assess its feasibility.
I can manually install a different version, but my project depends on google/apiclient, which, in its default configuration, throws warnings. That seems undesirable.
It's a side project for me, but assuming you mean Google with internal discussions, feel free to loop me in if I could provide further data.
Thanks for the reply! 馃檪
The default configuration would install the latest release in the 6.x tree. You鈥檇 need to explicitly require guzzle at a lower 6.x version somewhere else in the dependency tree to encounter the error. That鈥檚 why I鈥檓 unsure whether there鈥檚 any action needed on our end.
That said, we want to try and remove as many potential headaches as possible, given other constraints on time and backwards compatibility. So I will raise the possibility of bumping the minimum version of guzzle and see whether it鈥檚 doable in the immediate future.
The problem is in PHP 7.2 the parameter for count() can't be NULL. The warning gets displayed when $this->handles equals NULL. Just replace line 67 in CurlFactory.php with the following:
if (count( (array) $this->handles) >= $this->maxHandles) {
Thank you. solved the problems.
Hi @beverloo
Could you give me more information on what you'd like me to track? I'd be happy to reopen the issue if there's a need, but beyond potentially requiring guzzle 6.3+, I'm not sure if there's any action I can take.
The correct solution would be to upgrade to guzzle 6.3 or above. I'm not sure why anyone would be pinning to a version of guzzle 6 below that, but I think the only way this error would occur would be if for some reason you were stuck with an older version of guzzle for a reason unrelated to this library.
Perhaps we can update the minimum version for 6.x to 6.3.0. I'll need to discuss the implications of us doing that internally, but there's certainly nothing preventing you from requiring a higher version of guzzle within the 6.x series. The downloadable archives provide (for 2.4.0) guzzle 6.3.3.
Same problem with:
But it seems rather that https://developers.google.com/analytics/devguides/reporting/core/v4/quickstart/service-php is outdated
The quickstart sample codes don't set any dimensions, that's why.
Updated sample for https://developers.google.com/analytics/devguides/reporting/core/v4/quickstart/service-php:
<?php
// Load the Google API PHP Client Library.
require_once __DIR__ . '/vendor/autoload.php';
$analytics = initializeAnalytics();
$response = getReport($analytics);
printResults($response);
/**
* Initializes an Analytics Reporting API V4 service object.
*
* @return An authorized Analytics Reporting API V4 service object.
*/
function initializeAnalytics()
{
// Use the developers console and download your service account
// credentials in JSON format. Place them in this directory or
// change the key file location if necessary.
$KEY_FILE_LOCATION = __DIR__ . '/service-account-credentials.json';
// Create and configure a new client object.
$client = new Google_Client();
$client->setApplicationName("Hello Analytics Reporting");
$client->setAuthConfig($KEY_FILE_LOCATION);
$client->setScopes(['https://www.googleapis.com/auth/analytics.readonly']);
$analytics = new Google_Service_AnalyticsReporting($client);
return $analytics;
}
/**
* Queries the Analytics Reporting API V4.
*
* @param service An authorized Analytics Reporting API V4 service object.
* @return The Analytics Reporting API V4 response.
*/
function getReport($analytics) {
// Replace with your view ID, for example XXXX.
$VIEW_ID = "<REPLACE_WITH_VIEW_ID>";
// Create the DateRange object.
$dateRange = new Google_Service_AnalyticsReporting_DateRange();
$dateRange->setStartDate("7daysAgo");
$dateRange->setEndDate("today");
// Create the Metrics object.
$sessions = new Google_Service_AnalyticsReporting_Metric();
$sessions->setExpression("ga:sessions");
$sessions->setAlias("sessions");
//Create the Dimensions object.
$browser = new Google_Service_AnalyticsReporting_Dimension();
$browser->setName("ga:browser");
// Create the ReportRequest object.
$request = new Google_Service_AnalyticsReporting_ReportRequest();
$request->setViewId($VIEW_ID);
$request->setDateRanges($dateRange);
$request->setDimensions(array($browser));
$request->setMetrics(array($sessions));
$body = new Google_Service_AnalyticsReporting_GetReportsRequest();
$body->setReportRequests( array( $request) );
return $analytics->reports->batchGet( $body );
}
/**
* Parses and prints the Analytics Reporting API V4 response.
*
* @param An Analytics Reporting API V4 response.
*/
function printResults($reports) {
for ( $reportIndex = 0; $reportIndex < count( $reports ); $reportIndex++ ) {
$report = $reports[ $reportIndex ];
$header = $report->getColumnHeader();
$dimensionHeaders = $header->getDimensions();
$metricHeaders = $header->getMetricHeader()->getMetricHeaderEntries();
$rows = $report->getData()->getRows();
for ( $rowIndex = 0; $rowIndex < count($rows); $rowIndex++) {
$row = $rows[ $rowIndex ];
$dimensions = $row->getDimensions();
$metrics = $row->getMetrics();
for ($i = 0; $i < count($dimensionHeaders) && $i < count($dimensions); $i++) {
print($dimensionHeaders[$i] . ": " . $dimensions[$i] . "\n");
}
for ($j = 0; $j < count($metrics); $j++) {
$values = $metrics[$j]->getValues();
for ($k = 0; $k < count($values); $k++) {
$entry = $metricHeaders[$k];
print($entry->getName() . ": " . $values[$k] . "\n");
}
}
}
}
}
Just include the following code first and then your PHP code.
error_reporting(0);
This will not show any errors (no error reportings in the page/content) in PHP. Even you can limit error reporting by changing the
number of error reporting function. The format is error_reporting(int);.
Most helpful comment
The problem is in PHP 7.2 the parameter for count() can't be NULL. The warning gets displayed when $this->handles equals NULL. Just replace line 67 in CurlFactory.php with the following:
if (($this->handles ? count($this->handles) : 0) >= $this->maxHandles) {