Welcome Guest, Not a member yet? Register   Sign In
google index api integration
#1
Exclamation 
(This post was last modified: 05-08-2020, 12:42 PM by maverik23.)

Hi everyone, I am trying to integrate the google api to Codeigniter 3.x but I cannot solve the problems.

I have a copy of google / apiclient in the libraries folder
I have a file called google.php in the libraries folder
I have a controller called Google in the controllers folder


libraries/google.php

PHP Code:
<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

require_once 
APPPATH 'Google/Client.php';

class 
Google extends Google_Client {

    function __construct($params = array()) {

        parent::__construct();
    }



Controllers/Google.php
PHP Code:
<?php defined('BASEPATH') or exit('No direct script access allowed');

class 
Google extends CI_Controller
{
    public function __construct() {
        parent::__construct();
        $this->load->library('google');
    }
    
    
public function index()
    {
        echo $this->google->getLibraryVersion();
    }



The problem I have is that when entering the browser the Google controller gives me the error:

"Unable to locate the specified class: Session.php"

How can I solve this error?
Is there a library to use google?
Reply
#2

You are loading it incorrectly:
https://github.com/googleapis/google-api...he-release

Personally I would go with the composer package:
https://github.com/googleapis/google-api...t#composer

As CI 3.x supports it and you don't need to make a CI3 library. You should however make a library that generates all the data and just pass it to your controller so you can display it.

https://codeigniter.com/userguide3/gener...t=composer
Reply
#3

(05-08-2020, 12:46 PM)jreklund Wrote: You are loading it incorrectly:
https://github.com/googleapis/google-api...he-release

Personally I would go with the composer package:
https://github.com/googleapis/google-api...t#composer

As CI 3.x supports it and you don't need to make a CI3 library. You should however make a library that generates all the data and just pass it to your controller so you can display it.

https://codeigniter.com/userguide3/gener...t=composer


thanks, you helped me a lot. I was already able to solve my problem.
Reply
#4

Do you mind sharing the code that worked, as it will help other forum members.
Reply
#5

y change the code a little bit

this is my indexing model
PHP Code:
<?php  if (! defined('BASEPATH')) {
    exit('No direct script access allowed');
}

class Indexing extends CI_Model
{
    public function __construct()
    {
        parent::__construct();
    }
    
    public function AddUrl($url)
    {
        require APPPATH . 'libraries/google/vendor/autoload.php';
        $client = new Google_Client();

        $client->setAuthConfig(APPPATH . 'xxxxxxxxxxxxxxxxx.json');
        $client->addScope('https://www.googleapis.com/auth/indexing');
    
        $httpClient = $client->authorize();
        $endpoint = 'https://indexing.googleapis.com/v3/urlNotifications:publish';
    
        $content = '{
          "url": "' . $url . '",
          "type": "URL_UPDATED"
        }';
    
        $response = $httpClient->post($endpoint, [ 'body' => $content ]);
        $status_code = $response->getStatusCode();

        return $status_code;
    }

    public function DelUrl($url)
    {
        require APPPATH . 'libraries/google/vendor/autoload.php';
        $client = new Google_Client();

        $client->setAuthConfig(APPPATH . 'xxxxxxxxxxxxxxxxx.json');
        $client->addScope('https://www.googleapis.com/auth/indexing');
    
        $httpClient = $client->authorize();
        $endpoint = 'https://indexing.googleapis.com/v3/urlNotifications:publish';
    
        $content = '{
          "url": "' . $url . '",
          "type": "URL_DELETED"
        }';
    
        $response = $httpClient->post($endpoint, [ 'body' => $content ]);
        $status_code = $response->getStatusCode();
        
        return $status_code;
    }

    public function StatusUrl($url)
    {
        require APPPATH . 'libraries/google/vendor/autoload.php';
        $client = new Google_Client();

        $client->setAuthConfig(APPPATH . 'xxxxxxxxxxxxxxxxx.json');
        $client->addScope('https://www.googleapis.com/auth/indexing');
    
        $httpClient = $client->authorize();
        $endpoint = 'https://indexing.googleapis.com/v3/urlNotifications/metadata?url=' . $url;
    
        $response = $httpClient->get($endpoint);
        $status_code = $response->getStatusCode();
        
        return $status_code;
    }



and this is the code i added in my controller

PHP Code:
$this->load->model('Indexing');
$url base_url('xxxxxxxxxxxx');
$response $this->Indexing->addUrl($url); 
Reply




Theme © iAndrew 2016 - Forum software by © MyBB