未捕获的SoapFault异常:[Client]函数(“ getUserLists”)AdWords API

时间:2018-06-27 13:55:35

标签: php exception soap google-adwords google-api-php-client

每当我尝试运行示例时,都会返回以下输出:AddCrmBasedUserList.php。其他示例,例如GetAccountHierarchy.php和GetRefreshToken.php,都可以正常工作。

该示例部分起作用。它创建一个新的用户列表,但无法获取所有用户列表。如何解决此问题,有什么方法可以修改脚本以获取用户列表,而无需创建新列表。

User list with name 'Customer relationship management list #5b338ddd1c1224' and ID 6117212421 was added.
PHP Fatal error:  Uncaught SoapFault exception: [Client] Function ("getUserLists") is not a valid method for this service in \examples\AddCrmBasedUserList.php:131
Stack trace:
#0 Z:\examples\AddCrmBasedUserList.php(131): SoapClient->__call('getUserLists', Array)
#1 Z:\examples\AddCrmBasedUserList.php(131): Google\AdsApi\AdWords\v201710\rm\AdwordsUserListService->getUserLists()
#2 Z:\examples\AddCrmBasedUserList.php(151): Google\AdsApi\Examples\AdWords\v201710\Remarketing\AddCrmBasedUserList::runExample(Object(Google\AdsApi\AdWords\AdWordsServices), Object(Google\AdsApi\AdWords\AdWordsSession), Array)
#3 Z:\examples\AddCrmBasedUserList.php(155): Google\AdsApi\Examples\AdWords\v201710\Remarketing\AddCrmBasedUserList::main()
#4 {main}
  thrown in Z:\examples\AddCrmBasedUserList.php on line 131

代码:

<?php
/**
 * Copyright 2017 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

namespace Google\AdsApi\Examples\AdWords\v201710\Remarketing;

require __DIR__ . '/../vendor/autoload.php';

use Google\AdsApi\AdWords\AdWordsServices;
use Google\AdsApi\AdWords\AdWordsSession;
use Google\AdsApi\AdWords\AdWordsSessionBuilder;
use Google\AdsApi\AdWords\v201710\cm\Operator;
use Google\AdsApi\AdWords\v201710\rm\AddressInfo;
use Google\AdsApi\AdWords\v201710\rm\AdwordsUserListService;
use Google\AdsApi\AdWords\v201710\rm\CrmBasedUserList;
use Google\AdsApi\AdWords\v201710\rm\Member;
use Google\AdsApi\AdWords\v201710\rm\MutateMembersOperand;
use Google\AdsApi\AdWords\v201710\rm\MutateMembersOperation;
use Google\AdsApi\AdWords\v201710\rm\UserListOperation;
use Google\AdsApi\Common\OAuth2TokenBuilder;

/**
 * This example adds a user list (a.k.a. audience) and uploads members to
 * populate the list.
 *
 * <p><em>Note:</em> It may take up to several hours for the list to be
 * populated with members.
 * Email addresses must be associated with a Google account.
 * For privacy purposes, the user list size will show as zero until the list has
 * at least 1,000 members. After that, the size will be rounded to the two most
 * significant digits.
 */
class AddCrmBasedUserList
{

    private static $EMAILS = [
        'test@test.com',

    ];

    public static function runExample(
        AdWordsServices $adWordsServices,
        AdWordsSession $session,
        array $emails
    ) {
        $userListService = $adWordsServices->get($session, AdwordsUserListService::class);

        // Create a CRM based user list.
        $userList = new CrmBasedUserList();
        $userList->setName('Customer relationship management list #' . uniqid());
        $userList->setDescription(
            'A list of customers that originated from email addresses'
        );

        // Maximum life span is 180 days.
        $userList->setMembershipLifeSpan(180);

        // Create a user list operation and add it to the list.
        $operations = [];
        $operation = new UserListOperation();
        $operation->setOperand($userList);
        $operation->setOperator(Operator::ADD);
        $operations[] = $operation;

        // Create the user list on the server and print out some information.
        $userList = $userListService->mutate($operations)->getValue()[0];
        printf(
            "User list with name '%s' and ID %d was added.\n",
            $userList->getName(),
            $userList->getId()
        );

        // Create operation to add members to the user list based on email
        // addresses.
        $mutateMembersOperations = [];
        $mutateMembersOperation = new MutateMembersOperation();
        $operand = new MutateMembersOperand();
        $operand->setUserListId($userList->getId());

        $members = [];
        // Hash normalized email addresses based on SHA-256 hashing algorithm.
        foreach ($emails as $email) {
            $memberByEmail = new Member();
            $memberByEmail->setHashedEmail(self::normalizeAndHash($email));
            $members[] = $memberByEmail;
        }

        $firstName = 'test';
        $lastName = 'test';
        $countryCode = 'US';
        $zipCode = '10011';

        $addressInfo = new AddressInfo();
        // First and last name must be normalized and hashed.
        $addressInfo->setHashedFirstName(self::normalizeAndHash($firstName));
        $addressInfo->setHashedLastName(self::normalizeAndHash($lastName));
        // Country code and zip code are sent in plain text.
        $addressInfo->setCountryCode($countryCode);
        $addressInfo->setZipCode($zipCode);

        $memberByAddress = new Member();
        $memberByAddress->setAddressInfo($addressInfo);
        $members[] = $memberByAddress;

        // Add members to the operand and add the operation to the list.
        $operand->setMembersList($members);
        $mutateMembersOperation->setOperand($operand);
        $mutateMembersOperation->setOperator(Operator::ADD);
        $mutateMembersOperations[] = $mutateMembersOperation;

        // Add members to the user list based on email addresses.



        $userListService = $adWordsServices->get($session, AdwordsUserListService::class);

        foreach ($userListService->getUserLists() as $userList) {
            echo $userList->getName();
            echo $userList->getId();
        }
    }


    private static function normalizeAndHash($value)
    {
        return hash('sha256', strtolower(trim($value)));
    }

    public static function main()
    {
        // Generate a refreshable OAuth2 credential for authentication.
        $oAuth2Credential = (new OAuth2TokenBuilder())->fromFile()->build();

        // Construct an API session configured from a properties file and the
        // OAuth2 credentials above.
        $session = (new AdWordsSessionBuilder())->fromFile()->withOAuth2Credential($oAuth2Credential)->build();
        self::runExample(new AdWordsServices(), $session, self::$EMAILS);
    }
}

AddCrmBasedUserList::main();

0 个答案:

没有答案