Showing posts with label Google API. Show all posts
Showing posts with label Google API. Show all posts

Wednesday, April 30, 2014

Google AdSense API PHP Integration

Get More Money/Income from AdSense and Increase your Google Ads Revenue by Integrating and Customizing Ads using Google AdSense API. A sample GenerateAdCode method integration using NuSoap Client for Google AdSense API.

Source Code
<?php

require_once 'soapclient/nusoap.php';
require_once 'common.php';

$live  = FALSE;

define('EMAIL', '<YOUR-GOOGLE-DEVELOPER-EMAIL>');
define('PASSWORD', '<YOUR-GOOGLE-DEVELOPER-PASSWORD>');
define('CLIENT_ID', '<YOUR-GOOGLE-CLIENT-ID>');

if($live){
        $server = 'https://www.google.com';
    }else{
        $server = 'https://sandbox.google.com';
}

$namespace = 'http://www.google.com/api/adsense/v3';

/* Authentication Headers */

$header     = EMAIL . PASSWORD . CLIENT_ID;

/* Creating Soap Client Request */

$wsdl   = $server . '/api/adsense/v3/AccountService?wsdl';
$client = new soapclient($wsdl, true);
$err    = $client->getError();

if ($err) {
  showSoapClientError($err);
  return;
}
$client->soap_defencoding = 'UTF-8';

/* Set the headers; they are needed for authentication */
$client->setHeaders($header);
if ($client->fault) {
  showMyErrors($client);
  return;
}
$err = $client->getError();
if ($err) {
  showSoapClientError($err);
  return;
}

/* setting the parameter */
$param = "<ns1:getSyndicationService xmlns:ns1=\"$namespace\"><synServiceType><value>ContentAds</value></synServiceType></ns1:getSyndicationService>";

/* invoke web service */
showCall('getSyndicationService', $param);
$response = $client->call('getSyndicationService', $param, $namespace);
if ($client->fault) {
  showMyErrors($client);
  return;
}
$err = $client->getError();
if ($err) {
  showSoapClientError($err);
  return;
}

/* get back the response */
$response = $response['return'];
$synServiceId = $response['id'];
showSynServiceId($synServiceId);


/* showing the soap */
showRequestResponse($client);


// setup client soap for another webservice
$wsdl = $server . '/api/adsense/v3/AdSenseForContentService?wsdl';
$client = new soapclient($wsdl, true);
$err = $client->getError();
if ($err) {
  showSoapClientError($err);
  return;
}
$client->soap_defencoding = 'UTF-8';

/* Set the headers; they are needed for authentication */
$client->setHeaders($header);
if ($client->fault) {
  showMyErrors($client);
  return;
}
$err = $client->getError();
if ($err) {
  showSoapClientError($err);
  return;
}

/* setting up adstyle parameter */
$ads_param = '<adStyle>';
$ads_param .= "<name>demoStyle</name>";
$ads_param .= '<borderColor>#0000FF</borderColor>';
$ads_param .= '<backgroundColor>#FFFFFF</backgroundColor>';
$ads_param .= '<titleColor>#FF0000</titleColor>';
$ads_param .= '<textColor>#00FF00</textColor>';
$ads_param .= '<urlColor>#FFFF00</urlColor>';
$ads_param .= '</adStyle>';

/* setting up parameter */
$param = '<synServiceId>'.$synServiceId.'</synServiceId>';
$param .= $ads_param;
$param .= '<adUnitType><value>TextOnly</value></adUnitType>';
$param .= '<layout><value>728x90</value></layout>';
$param .= '<alternate>#FFFFFF</alternate>';
$param .= '<isFramedPage type="xsd:boolean">false</isFramedPage>';
$param .= '<channelName xsi:nil="true"/>';
$param .= '<cornerStyles><value>DEFAULT</value></cornerStyles>';

$param = "<ns1:generateAdCode xmlns:ns1=\"$namespace\">" . $param . "</ns1:generateAdCode>";

/* invoke the web service */
showCall('generateAdCode', $param);
$response = $client->call('generateAdCode', $param, $namespace);
if ($client->fault) {
  showMyErrors($client);
  return;
}
$err = $client->getError();
if ($err) {
  showSoapClientError($err);
  return;
}

$response = $response['return'];
$snippet = $response;
showSnippet($snippet);

/* showing the soap */
showRequestResponse($client);

?>


Simple Google URL Shortener PHP Class Wrapper

This is a simple PHP Class Wrapper using Google API URL

<?php

class GoogleUrlShortener 
{
        public $endPoint = 'https://www.googleapis.com/urlshortener/v1/url';
        public $key      = 'YOUR_GOOGLE_API_KEY';
        
        /* Shorten a long URL */
        function shorten($url)
        {
            $data = $this->execute($url,1);
            
            if(isset($data['id'])){
                    $result = $data['id'];
                }else{
                    $result = FALSE;
            }
            
            return $result;
        }
        
        /* Expand a short URL */
        function expand($url) 
        {
            $data = $this->execute($url,0);
            
            if(isset($data['longUrl'])){
                    $result = $data['longUrl'];
                }else{
                    $result = FALSE;
            }
            
            return $result;
        }
        
        /* Send Request to Google */
        function execute($url,$status) 
        {
            $curl           = curl_init();
            $googleApiUrl   = $this->endPoint.'?key='.$this->key;
            
            if($status == 1) {
                    curl_setopt($curl,CURLOPT_URL,$googleApiUrl);
                    curl_setopt($curl,CURLOPT_POST,1);
                    curl_setopt($curl,CURLOPT_POSTFIELDS,json_encode(array("longUrl"=>$url)));
                    curl_setopt($curl,CURLOPT_HTTPHEADER,array("Content-Type: application/json"));
                }else {
                    curl_setopt($curl,CURLOPT_URL,$googleApiUrl.'&shortUrl='.$url);
            }
            
            curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
            $res = curl_exec($curl);
            curl_close($curl);
            
            return json_decode($res,true);
        }    
}

Examples
  • Shorten URL
<?php

$shorten_my_url = new GoogleUrlShortener();
echo $shorten_my_url->shorten("http://wpmigrator.wordpress.com");
output : http://goo.gl/MZ4bW
  • Expand URL
<?php

$expand_my_url = new GoogleUrlShortener();
echo $expand_my_url->expand("http://goo.gl/MZ4bW");
output : http://wpmigrator.wordpress.com
Genesis Framework for WordPress