Link to home
Start Free TrialLog in
Avatar of Marcusw
Marcusw

asked on

How to call / run an API using PHP

I am trying to connect my website to our emailng servce.  It provides an API which allows functions to be called.  However, they do not explain how to call the API function.

I have a list of functions and i can enter them into a browser address bar and they work, but i do not know how i could execute them from a button or link.  Also some of them return xml docs which i would need to deal with

an example function would be

get_dataset
 
 
Overview
Returns the top 10 000 columns in XML, or the top 30 000 columns in CSV format defined for a specific dataset. The amount of data you can retrieve is limited, because of the amount of data that needs to be transferred
 
Parameters
 
Name      Type      Required      Comments
Csv      Boolean      No      Set to true if you want the data to be returned in csv format
DatasetID      Integer      Yes      The id of the dataset you want returned
Returns
0|Error (Different descriptions for the error)
XML
  "  dataset name=DatasetName
    "  entry
      "  emailaddress
      "  mobilenumber
      "  password
      "  column name=Column1Name
      "  &
      "  column name=Column25Name

Or the data in csv format
Example

https://www.graphicmail.co.uk/api.aspx?Username=user@mydomain.com&Password=123456&Function=get_dataset&DatasetID=123456Csv=true&SID=6

The page where the api is described is
http://www.graphicmail.co.uk/api/documentation/

Thanks in Advance
Avatar of Kalpan
Kalpan
Flag of India image

Please refer the attached code...

You need to set the CURL library for unix/Linux or windows with LAMP/WAMP architecture or check that if you have already installed...This is required to call the API with the PHP egnine on your server....

you can change the parameters fro more functions and data set for the csv or other formatted of the data..

Let me know if you need more help...

Hope this will be helpful...

Thanks

Kalpan
Private function create_post_string($method, $params) {
  	$params['Username'] = $username;
  	$params['Password'] = $password;
  	$params['Function'] = $your_function; // to retrive the dataset
  	$params['Csv'] = true;
  	$params['SID'] = 6;
  	
  	
    $post_params = array();
    foreach ($params as $key => &$val) {
      if (is_array($val)) $val = implode(',', $val);
      $post_params[] = $key.'='.urlencode($val);
    }
    return implode('&', $post_params);
  }


public function post_request($method, $params) {

$post_string = $this->create_post_string($method, $params);

$ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, "https://www.graphicmail.co.uk/api.aspx ");
      curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      $result = curl_exec($ch);
      curl_close($ch);
}

Open in new window

Avatar of Marcusw
Marcusw

ASKER

thanks for the code, i have put it in a page, but how do i get it to run?
well you would need to put this in the file where you could save that as .php and than run that file on your server host url.....

You can create the class in php file to member functions as above in the code and than call that class with test.php to get the output in CSV...

thanks

Kalpan
Please create the files at your root level directory of your webserver

graphicmailapi.class.php

testgraphicmail.php

now call the testgraphicmail.php....you will be able to get the CSV output...

Hope it will be convincing...

thanks

Kalpan
// graphicmailapi.class.php


class GraphimailAPI{

Private function create_post_string($params) {
    $post_params = array();
    foreach ($params as $key => &$val) {
      if (is_array($val)) $val = implode(',', $val);
      $post_params[] = $key.'='.urlencode($val);
    }
    return implode('&', $post_params);
  }


public function post_request($params) {

$post_string = $this->create_post_string($method, $params);

if (function_exists('curl_init')) {
$ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, "https://www.graphicmail.co.uk/api.aspx ");
      curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      $result = curl_exec($ch);
      curl_close($ch);
}
}else{
   echo "Please install CURL library";
   exit;
}

}


// testgraphicmail.php

include 'graphicmailapi.class.php';

$params = new array();

$params['Username'] = $username;
$params['Password'] = $password;
$params['Function'] = $your_function; // to retrive the dataset
$params['Csv'] = true;
$params['SID'] = 6;

$objMailApi = new GraphimailAPI();

$result = $objMailApi->post_request($params);

echo $result;

Open in new window

Avatar of Marcusw

ASKER

Hi, i have create the 2 file in my root directory and run the testgraphicmail.php but i get the following error

Parse error: syntax error, unexpected T_ARRAY, expecting T_STRING or T_VARIABLE or '$' in /home/t/e/tester/public_html/testgraphicmail.php on line 4
Sorry, i had to check the code on the run....

now it should be ok when you use your user name, password....and the other parameters on the testgraphicmail.php and run on ur server.

thanks

Kalpan


// graphicmailapi.class.php

<?php

class GraphimailAPI{

Private function create_post_string($params) {
    $post_params = array();
    foreach ($params as $key => &$val) {
      if (is_array($val)) $val = implode(',', $val);
      $post_params[] = $key.'='.urlencode($val);
    }
    return implode('&', $post_params);
  }


public function post_request($params) {

        $post_string = $this->create_post_string($params);

        if (function_exists('curl_init')) {
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL, "https://www.graphicmail.co.uk/api.aspx");
                curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                $result = curl_exec($ch);
                curl_close($ch);
        }else{
                echo "Please install CURL library";
                exit;
        }
}
}
?>

// testgraphicmail.php


<?php

include 'graphicmailapi.class.php';

$params = array();

$params['Username'] = $username;
$params['Password'] = $password;
$params['Function'] = $your_function; // to retrive the dataset
$params['Csv'] = true;
$params['SID'] = 6;

$objMailApi = new GraphimailAPI();

$result = $objMailApi->post_request($params);

echo $result;

?>

Open in new window

Avatar of Marcusw

ASKER

Still no luck, i just get a blank screen
did u put the correct username and password and the function that you would like to use ?

it will show if the correct information has been provided to the testgraphicmail.php in the params

sorry, i can't really get the output as i don't have the credentails for that...

hope this is convincing..

thanks

Kalpan
Avatar of Marcusw

ASKER

I have echoed $post_stings and the information inputed is correct but the username which is an email address is showing %40 instead of @ which could be the problem.  Is there a way to change this
well, check the url that you've provided earlier...

https://www.graphicmail.co.uk/api.aspx?Username=user@mydomain.com&Password=123456&Function=get_dataset&DatasetID=123456Csv=true&SID=6

try replacing %40 instead of @....i don't think so it would make ny issues....

see if you get the data for this url with correct username and password....you should be able to get the data with testgraphicmail.php in CSV...

Kalpan


Avatar of Marcusw

ASKER

I have now checked the url with the %40 and it works.

If i echo the $post_strings and pasted this onto the beginning of the url in the address bar and this also works, so the string being created is correct.  but when i put this in the code you have supplied i still do not get anything but a blank page
ASKER CERTIFIED SOLUTION
Avatar of Kalpan
Kalpan
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Marcusw

ASKER

Hi I Still seemd to be getting errors, after some research i have made it work with the attached code.  I have added

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,  2);


 I will open another question on how to run this from a form button / link

Many thanks for your help



<?php

class GraphimailAPI{

Private function create_post_string($params) {
    $post_params = array();
    foreach ($params as $key => &$val) {
      if (is_array($val)) $val = implode(',', $val);
      $post_params[] = $key.'='.urlencode($val);
    }
    return implode('&', $post_params);
  }


public function post_requestString($params) {

        $post_string = $this->create_post_string($params);
        if (function_exists('curl_init')) {
              
			  
		$ch = curl_init("https://www.graphicmail.co.uk/api.aspx");
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
	       curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,  2); 
                curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

		$result = curl_exec($ch);
                return $result;
	       curl_close($ch);
				
        }else{
                echo "Please install CURL library";
                exit;
        }
}

public function post_requestRaw($params) {

        $post_string = $this->create_post_string($params);
        if (function_exists('curl_init')) {
              
			  
			    $ch = curl_init("https://www.graphicmail.co.uk/api.aspx");
				curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
				curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,  2); 
                curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);

			     $result = curl_exec($ch);
                return $result;
				curl_close($ch);
				
        }else{
                echo "Please install CURL library";
                exit;
        }
}
}

?>

Open in new window