1

I am trying to use Google-API-PHP-Client and its base class is throwing following error:

Severity: Warning

Message: array_merge() [function.array-merge]: Argument #1 is not an array

Filename: libraries/Google_Client.php

Line Number: 107

Code around like 107 is something like:

public function __construct($config = array()) {
    global $apiConfig;
    $apiConfig = array_merge($apiConfig, $config);
    self::$cache = new $apiConfig['cacheClass']();
    self::$auth = new $apiConfig['authClass']();
    self::$io = new $apiConfig['ioClass']();
  }

I understand that global $apiConfig is not initialized as array that's why array_merge is throwing error. But when I change it to global $apiConfig = array();, got another error Parse error: syntax error, unexpected '=', expecting ',' or ';' in C:\Softwares\xampp\htdocs\testsaav\application\libraries\Google_Client.php on line 106

I am using Codeigniter 2.3 with XAMPP which has PHP 5.3

2 Answers 2

2

Initialize your array in your function (if necessary)

public function __construct($config = array()) {
    global $apiConfig;
    $apiConfig = (isset($apiConfig) && is_array($apiConfig)) ? $apiConfig : array(); // initialize if necessary
    $apiConfig = array_merge($apiConfig, $config);
    self::$cache = new $apiConfig['cacheClass']();
    self::$auth = new $apiConfig['authClass']();
    self::$io = new $apiConfig['ioClass']();
  }
Sign up to request clarification or add additional context in comments.

5 Comments

It worked for me. Thanks. Can you please explain why global $apiConfig = array(); didn't work at the first place?
You can't assign a value to a variable while declaring it global.
Because you were trying to declare you wanted the global at the same time you were wiping it out. Secondly this is Google's code that you are modifying. Probably not the solution that you want. You need to find out why the code is not loading the $apiConfig created in config.php
I guess because Google's API is not compatible with PHP 5.3. Everything worked fine after this change.
What you've done is just to bypass the fact that the configuration was not loaded. The Google wiki says the requirements are PHP 5.3 or greater.
2

Check your server logs and see if there is an error related to the require_once('config.php') in Google_Client.php (If the file wasn't found, the script should have stopped).

When you do your require_once('Google_Client.php'), the following code is executed from that file. After you do your require, $apiConfig should be visible to your script.

// hack around with the include paths a bit so the library 'just works'
set_include_path(dirname(__FILE__) . PATH_SEPARATOR . get_include_path());

require_once "config.php";
// If a local configuration file is found, merge it's values with the default configuration
if (file_exists(dirname(__FILE__)  . '/local_config.php')) {
  $defaultConfig = $apiConfig;
  require_once (dirname(__FILE__)  . '/local_config.php');
  $apiConfig = array_merge($defaultConfig, $apiConfig);
}

Note that you do not touch config.php. If you need to override anything in there, you create local_config.php.

From my system with PHP 5.3 I used this script. The script as show below throws no errors. Unsetting the $apiConfig replicates your error.

<?php

require_once('src/Google_Client.php');

print_r($apiConfig);
// uncommenting the next line replicates issue.
//unset($apiConfig);
$api = new Google_Client();

?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.