googleapis / google-api-php-client

A PHP client library for accessing Google APIs
Apache License 2.0
9.22k stars 3.52k forks source link

Fatal error: Uncaught Error: Call to undefined method Google\Service\Indexing\PublishUrlNotificationResponse::getExecutionStatus() in #2433

Closed ajansdog2121 closed 12 months ago

ajansdog2121 commented 1 year ago

"Fatal error: Uncaught Error: Call to undefined method Google\Service\Indexing\UrlNotificationMetadata::getMetadata() in"

I am getting an error like this. The php version I am using is 7.4 and I have downloaded the appropriate library for this version. I have prepared PHP build. I have created a Google Api. But I am encountering this error. The content of my php file is as follows; <?php error_reporting(E_ALL); ini_set('display_errors', 1);

require_once DIR . '/path/vendor/autoload.php';

use Google\Client; use Google\Service\Indexing; use Google\Service\Indexing\UrlNotification; try { // Google API Credentials $google_api_key_file = './path/googleapi.json';

// Google_Client create your object
$client = new Google\Client();
$client->setAuthConfig($google_api_key_file);
$client->addScope("https://www.googleapis.com/auth/indexing");

// Authenticate
$client->fetchAccessTokenWithAssertion();

// Google_Service_Indexing create your object
$service = new Google\Service\Indexing($client);

// Get the last 10 content
$xml = simplexml_load_file('path/sitemap.xml');
$urls = array();
foreach ($xml->url as $url) {
    if (count($urls) >= 10) break;
    $urlNotification = new UrlNotification();
    $urlNotification->setUrl((string)$url->loc);
    $urlNotification->setType("URL_UPDATED");

    $response = $service->urlNotifications->publish($urlNotification);
    $metadata = $response->getUrlNotificationMetadata();
    $status = $metadata->getMetadata(['status']);

    if ($status === 'URL_NOTIFICATIONS_COMPLETE') {
        $urls[] = (string)$url->loc;
    }
}

// Show successful or unsuccessful result
if (!empty($urls)) {
    echo "Successful! The last 10 items are:<br>";
    echo "<ul>";
    foreach (array_slice($urls, -10) as $url) {
        echo "<li>" . $url . "</li>";
    }
    echo "</ul>";
} else {
    echo "Unsuccessful! No URLs reported.";
}

// Save and view notification status
logNotificationStatus($urls, $status);

$filename = "googleapi-log.txt";
$statusMessage = ($status === 'URL_NOTIFICATIONS_COMPLETE') ? "SUCCESS" : "FAILED";

// Get date and time information
$currentDateTime = date("Y-m-d H:i:s");

// Save operation result to file
$logMessage = "[$currentDateTime] Transaction Result: $statusMessage" . PHP_EOL;
file_put_contents($filename, $logMessage, FILE_APPEND);

} catch (Google\ApiCore\ApiException $e) { $errorMessage = $e->getMessage(); if (strpos($errorMessage, "RATE_LIMIT_EXCEEDED") !== false) { echo "You have exceeded your daily limit. Please try another day."; } else { echo "Hata: " . $errorMessage; } }

// Get date and time information $currentDateTime = date("Y-m-d H:i");

// Print date and time information to the screen echo "
"; echo "Last run date: " . $currentDateTime;

// Save date and time to file $file = 'googleapi-log.txt'; $current = file_get_contents($file); $current .= "date: " . $currentDateTime . "\n"; file_put_contents($file, $current);

?>

LindaLawton commented 1 year ago

Would you be so kind as to fix the formatting on your question.

yash30201 commented 1 year ago

@ajansdog2121 I tried formatting the code to understand but the code itself is incomplete. Can you please provide a repro case so than I can replicate this on my end?

Moreover, there has been a new release on 18th May 2023 v.2.15.0 (link). Can you please check if you encounter the same with this?

yash30201 commented 12 months ago

Closing this issue due to inactivity.