?
Solved

How to call / run an API using PHP

Posted on 2010-01-11
13
Medium Priority
?
613 Views
Last Modified: 2013-12-12
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
0
Comment
Question by:Marcusw
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
13 Comments
 
LVL 14

Expert Comment

by:Kalpan
ID: 26282123
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

0
 

Author Comment

by:Marcusw
ID: 26282162
thanks for the code, i have put it in a page, but how do i get it to run?
0
 
LVL 14

Expert Comment

by:Kalpan
ID: 26282222
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
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 14

Expert Comment

by:Kalpan
ID: 26282261
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

0
 

Author Comment

by:Marcusw
ID: 26282283
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
0
 
LVL 14

Expert Comment

by:Kalpan
ID: 26282336
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

0
 

Author Comment

by:Marcusw
ID: 26282640
Still no luck, i just get a blank screen
0
 
LVL 14

Expert Comment

by:Kalpan
ID: 26282753
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
0
 

Author Comment

by:Marcusw
ID: 26282810
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
0
 
LVL 14

Expert Comment

by:Kalpan
ID: 26282869
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


0
 

Author Comment

by:Marcusw
ID: 26297787
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
0
 
LVL 14

Accepted Solution

by:
Kalpan earned 2000 total points
ID: 26300816
Please replace the attached code of the graphicmailapi.class.php....it should return the output for sure....and make sure you have your credentials set on testgraphicmail.php like below....

$params['Username'] = 'kalpan@test.com';
$params['Password'] = 'pass';
$params['Function'] = 'get_dataset'; // to retrive the dataset
$params['Csv'] = true;
$params['SID'] = 6;


I got this output when i tried...

0|Authentication failed. Check username and password.

so you would definitely get your output now with correct user credentials

Thanks, Kalpan
<?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);
                return $result;
        }else{
                echo "Please install CURL library";
                exit;
        }
}
}
?>

Open in new window

0
 

Author Comment

by:Marcusw
ID: 26302643
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

0

Featured Post

WordPress Tutorial 2: Terminology

An important part of learning any new piece of software is understanding the terminology it uses. Thankfully WordPress uses fairly simple names for everything that make it easy to start using the software.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question