Help Center Manage Your Account AdSpeed API

Sample Code

Please see the example below to access AdSpeed API.

PHP

<?php
class MyApp {
    private $mEndPoint = 'https://api.adspeed.com/';
    private $mKey = 'YOUR_KEY'; // change to your own key
    private $mSecret = 'YOUR_SECRET'; // change to your own secret
 
    /**
    * Request API method via GET
    * @param string method
    * @param array params
    * @return string
    */
    public function getAPI($pMethod,array $pParams=array()) {
        $vParams = array('key'=>$this->mKey,'method'=>$pMethod);
        $vParams = array_merge($pParams,$vParams);
        ksort($vParams); // sort the params alphabetically
        $vParams['sig'] = md5($this->mSecret.http_build_query($vParams,'','&'));
        $vURL = $this->mEndPoint.'?'.http_build_query($vParams,'','&');
         
        $vCURL = curl_init($vURL);
        curl_setopt($vCURL,CURLOPT_USERAGENT,'AdSpeed-API-Requester');
        curl_setopt($vCURL,CURLOPT_RETURNTRANSFER,1); // get XML result as string
        $vResult = curl_exec($vCURL);
        curl_close($vCURL);
        return $vResult;
    }
 
    /**
    * Request API method via POST
    * @param string method
    * @param array params
    * @return string
    */
    public function postAPI($pMethod,array $pParams=array()) {
        $vParams = array('key'=>$this->mKey,'method'=>$pMethod);
        $vParams = array_merge($pParams,$vParams);
        ksort($vParams); // sort the params alphabetically
        $vParams['sig'] = md5($this->mSecret.http_build_query($vParams,'','&'));
         
        $vCURL = curl_init($this->mEndPoint);
        curl_setopt($vCURL,CURLOPT_POST,1); // use POST instead of GET
        curl_setopt($vCURL,CURLOPT_POSTFIELDS,$vParams);
        curl_setopt($vCURL,CURLOPT_USERAGENT,'AdSpeed-API-Requester');
        curl_setopt($vCURL,CURLOPT_RETURNTRANSFER,1); // get XML result as string
        $vResult = curl_exec($vCURL);
        curl_close($vCURL);
        return $vResult;
    }

	/**
    * Upload an image (require Fast Delivery paid add-on)
    * @param string full path and filename
    * @param string description
    * @return string
    */
	public function uploadImageAPI($pFullFileName,$pDescr) {
        $vParams = array(
            'key'=>$this->mKey,
            'method'=>'AS.Medias.upload',
            'descr'=>$pDescr,
        );
        ksort($vParams); // sort the params alphabetically
        $vParams['sig'] = md5($this->mSecret.http_build_query($vParams,'','&'));

        $vParams['file'] = '@'.$pFullFileName; // should be filename with full absolute path

        $vCURL = curl_init($this->mEndPoint);
        curl_setopt($vCURL,CURLOPT_POST,1);
        curl_setopt($vCURL,CURLOPT_POSTFIELDS,$vParams);
        curl_setopt($vCURL,CURLOPT_USERAGENT,'AdSpeed-API-Requester');
        curl_setopt($vCURL,CURLOPT_RETURNTRANSFER,1); // get XML result as string
        $vResult = curl_exec($vCURL);
        curl_close($vCURL);
        return $vResult;
    }
     
    /**
    * Sample usage
    * @return void
    */
    public function example() {
        // GET      
        $vResult = $this->getAPI('AS.Ads.getCounts');
        var_dump($vResult);
     
		/*
		// POST
        $vParams = array(
            'name'=>'TextAdViaAPI',
            'first'=>'First Text Line',
            'next'=>'Second Line',
            'clickurl'=>'https://example.com',
        );
        $vResult = $this->postAPI('AS.Ads.createText',$vParams);
        var_dump($vResult);

		// UPLOAD (FAST DELIVERY ADD-ON NEEDED)
		$vResult = $this->uploadImageAPI('api.png','Upload an image ad file');
		var_dump($vResult);
		*/
    }
}
 
$vTest = new MyApp();
$vTest->example();
 
?>

Token Hash Generation

In certain methods that deal directly with one or more entities, a "token" parameter is required to verify ownership. The hash is generated by concatenating all the required fields into a single string and generating the MD5 hash for that string. Here is an example to generate token with format [Ad ID][Ad Name][Zone ID][Zone Name]:
Ad ID = '1234'
Ad Name = 'MyTestAd'
Zone ID = '789' 
Zone Name = 'MyTestZone'

token = md5('1234MyTestAd789MyTestZone') 
token = '971b6ebe7580f203905a0bf3fde9fd7b'

Other Articles in AdSpeed API

Documentation to use AdSpeed API to integrate AdSpeed AdServer into your own platform.

Cannot find an answer for your question? Ask our Customer Care team

Related