AdWords is now Google Ads. Our new name reflects the full range of advertising options we offer across Search, Display, YouTube, and more. Learn more

My Business
3.9K members online now
3.9K members online now
For developers who are using the Google My Business API to manage locations
Guide Me
star_border
Reply

Unable to create new location : Class 'Google_Service_Mybusiness_CreateLocationRequest' not found

[ Edited ]
Follower ✭ ☆ ☆
# 1
Follower ✭ ☆ ☆

Hi i'm trying to add location via MyBusiness API (v3) using OAuth 2.0 flow + google-api-php-client +Mybusiness PHP Classes. 

I can get locations and information, but can not create new location.

 

 

Here what i use

 

include_once "autoload.php";
include_once  "api/Mybusiness.php";

/*
/*OAuth 2.0 flow
/*

 

 

$aLocationInfo = array();
$aLocationInfo['AddressLines'] = "Address information";
$aLocationInfo['Locality'] = "Local information";
#... all other information here ...

$mybusinessService = new Google_Service_Mybusiness($client);
$locations = $mybusinessService->accounts_locations;
$aAccounts = $mybusinessService->accounts->listAccounts();
$account = $aAccounts[0];


$oLocation = createLocation($locations, $account,$aLocationInfo);

 

 

 

#I have the function 

 

function createLocation($locations, $account,$aLocationInfo)
{
  $location = new Google_Service_Mybusiness_Location();
  $address = new Google_Service_Mybusiness_Address();
  $address->setAddressLines($aLocationInfo['AddressLines']);
  $address->setLocality($aLocationInfo['Locality']);
  $address->setAdministrativeArea($aLocationInfo['AdministrativeArea']);
  $address->setCountry($aLocationInfo['Country']);
  $address->setPostalCode($aLocationInfo['PostalCode']);
  $location->setAddress($address);
  $businessHours = new Google_Service_Mybusiness_BusinessHours();
  $timePeriods = array();
  $days = $aLocationInfo['days'];
  foreach ($days as $day) {
    $timePeriod = new Google_Service_Mybusiness_TimePeriod();
    $timePeriod->setOpenDay($day);
    $timePeriod->setOpenTime($aLocationInfo['OpenTime']);
    $timePeriod->setCloseTime($aLocationInfo['CloseTime']);
    $timePeriod->setCloseDay($day);
    $timePeriods[] = $timePeriod;
  }
  $businessHours->setPeriods($timePeriods);
  $location->setRegularHours($businessHours);
  $location->setLocationName($aLocationInfo['LocationName']);
  $location->setStoreCode($aLocationInfo['StoreCode']);
  $location->setPrimaryPhone($aLocationInfo['PrimaryPhone']);
  $primaryCategory = new Google_Service_Mybusiness_Category();
  $primaryCategory->setName($aLocationInfo['primaryCategory']);
  $location->setPrimaryCategory($primaryCategory);
  $additionalCategories = $aLocationInfo['additionalCategories'];
  $location->setAdditionalCategories($additionalCategories);
  $location->setWebsiteUrl($aLocationInfo['WebsiteUrl']);
  $labels = $aLocationInfo['labels'];
  $location->setLabels($labels);
  $createLocationRequest = new Google_Service_Mybusiness_CreateLocationRequest();
  $createLocationRequest->setLocation($location);
  $createLocationRequest->setLanguageCode($aLocationInfo['LanguageCode']);
  $createLocationRequest->setValidateOnly(FALSE);
  $createLocationRequest->setRequestId("1a84939c-ab7d-4581-8930-ee35af6fefac");
  try {
    $createdlocation = $locations->create($account->name, $createLocationRequest);
  } catch (Exception $e) {
    print "Caught exception: " . $e->getMessage() . "\n";
    return null;
  }

  return $createdlocation;
}

 

 

 

but the class "Google_Service_Mybusiness_CreateLocationRequest" is not found.

I took this function from the https://groups.google.com/forum/#!searchin/my-business-api/Google_Service_Mybusiness_CreateLocationR...

 

and i can't find such a class or information nowhere 

 

Please give me a advice.

 

Thank you!

 

 

1 Expert replyverified_user
1 ACCEPTED SOLUTION

Accepted Solutions
Marked as Best Answer.
Solution
Accepted by topic author Iliyan H
May 2016

Re: Unable to create new location : Class 'Google_Service_Mybusiness_CreateLocationRequest' not foun

Google Employee
# 2
Google Employee

Hi @Iliyan H,

 

The source code you provided in the post is for v2 of the Google My Business API. The format of the HTTP request including Query parameters and Request body for creating a new location or updating a location has changed for v3 of the Google My Business API.

 

The following is a code snippet for creating a new location via v3 of the Google My Business API with the PHP client library:

// Creates a new location owned by the specified account, and returns it.

function createLocation($locations, $account)

{

 $location = new Google_Service_Mybusiness_Location();

 $address = new Google_Service_Mybusiness_Address();

 $address->setAddressLines("Level 5, 48 Pirrama Road");

 $address->setLocality("Pyrmont");

 $address->setAdministrativeArea("NSW");

 $address->setCountry("AU");

 $address->setPostalCode("2009");

 $location->setAddress($address);

 $businessHours = new Google_Service_Mybusiness_BusinessHours();

 $timePeriods = array();

 $days = array("Monday", "Tuesday", "Wednesday", "Thursday", "Friday");

 foreach ($days as $day) {

   $timePeriod = new Google_Service_Mybusiness_TimePeriod();

   $timePeriod->setOpenDay($day);

   $timePeriod->setOpenTime("9:00");

   $timePeriod->setCloseTime("17:00");

   $timePeriod->setCloseDay($day);

   $timePeriods[] = $timePeriod;

 }

 $businessHours->setPeriods($timePeriods);

 $location->setRegularHours($businessHours);

 $location->setLocationName("Google Sydney");

 $location->setStoreCode("GOOG-SYD");

 $location->setPrimaryPhone("02 9374 4000");

 $primaryCategory = new Google_Service_Mybusiness_Category();

 $primaryCategory->setName("Software Company");

 $primaryCategory->setCategoryId("gcid:software_company");

 $location->setPrimaryCategory($primaryCategory);

 $additionalCategories = array(

   array(

     "name" => "Computer Service",

     "categoryId" => "gcid:computer_service"

   ),

   array(

     "name" => "Corporate Office",

     "categoryId" => "gcid:corporate_office"

   )

 );

 $location->setAdditionalCategories($additionalCategories);

 $location->setWebsiteUrl("https://www.google.com.au/");

 $labels = array("Recently opened", "High-traffic");

 $location->setLabels($labels);

 

 try {

   $createdlocation = $locations->create($account->name, $location, array('languageCode' => 'en-AU', 'validateOnly' => FALSE, 'requestId' => uniqid()));

 } catch (Exception $e) {

   print "Caught exception: " . $e->getMessage() . "\n";

   return null;

 }

 

 return $createdlocation;

}

 

I hope this helps solve your problem.

 

Thanks, 

Terry

View solution in original post

Marked as Best Answer.
Solution
Accepted by topic author Iliyan H
May 2016

Re: Unable to create new location : Class 'Google_Service_Mybusiness_CreateLocationRequest' not foun

Google Employee
# 2
Google Employee

Hi @Iliyan H,

 

The source code you provided in the post is for v2 of the Google My Business API. The format of the HTTP request including Query parameters and Request body for creating a new location or updating a location has changed for v3 of the Google My Business API.

 

The following is a code snippet for creating a new location via v3 of the Google My Business API with the PHP client library:

// Creates a new location owned by the specified account, and returns it.

function createLocation($locations, $account)

{

 $location = new Google_Service_Mybusiness_Location();

 $address = new Google_Service_Mybusiness_Address();

 $address->setAddressLines("Level 5, 48 Pirrama Road");

 $address->setLocality("Pyrmont");

 $address->setAdministrativeArea("NSW");

 $address->setCountry("AU");

 $address->setPostalCode("2009");

 $location->setAddress($address);

 $businessHours = new Google_Service_Mybusiness_BusinessHours();

 $timePeriods = array();

 $days = array("Monday", "Tuesday", "Wednesday", "Thursday", "Friday");

 foreach ($days as $day) {

   $timePeriod = new Google_Service_Mybusiness_TimePeriod();

   $timePeriod->setOpenDay($day);

   $timePeriod->setOpenTime("9:00");

   $timePeriod->setCloseTime("17:00");

   $timePeriod->setCloseDay($day);

   $timePeriods[] = $timePeriod;

 }

 $businessHours->setPeriods($timePeriods);

 $location->setRegularHours($businessHours);

 $location->setLocationName("Google Sydney");

 $location->setStoreCode("GOOG-SYD");

 $location->setPrimaryPhone("02 9374 4000");

 $primaryCategory = new Google_Service_Mybusiness_Category();

 $primaryCategory->setName("Software Company");

 $primaryCategory->setCategoryId("gcid:software_company");

 $location->setPrimaryCategory($primaryCategory);

 $additionalCategories = array(

   array(

     "name" => "Computer Service",

     "categoryId" => "gcid:computer_service"

   ),

   array(

     "name" => "Corporate Office",

     "categoryId" => "gcid:corporate_office"

   )

 );

 $location->setAdditionalCategories($additionalCategories);

 $location->setWebsiteUrl("https://www.google.com.au/");

 $labels = array("Recently opened", "High-traffic");

 $location->setLabels($labels);

 

 try {

   $createdlocation = $locations->create($account->name, $location, array('languageCode' => 'en-AU', 'validateOnly' => FALSE, 'requestId' => uniqid()));

 } catch (Exception $e) {

   print "Caught exception: " . $e->getMessage() . "\n";

   return null;

 }

 

 return $createdlocation;

}

 

I hope this helps solve your problem.

 

Thanks, 

Terry

Re: Unable to create new location : Class 'Google_Service_Mybusiness_CreateLocationRequest' not foun

[ Edited ]
Follower ✭ ☆ ☆
# 3
Follower ✭ ☆ ☆

Hi Terry thank you for the answer. It works !