Drupal 7 Services Example - PHP Curl

I had a need to consume a custom service I created in Drupal services. There's loads of tutorials on how to do that, feel free to google. But my previous code for using the api I created was for D6, a few things have changed. Looking around I found most examples were javascript, or using drupal's http library. I wrote a tiny little object just as a simple way to consume using curl for reference. It's not complete, but it lets you log in and out. You can take it from there to build what you need.

  1. /**
  2.  * Returns an api class for use
  3.  */
  4. class ApiConsumer {
  5.   const API_ENDPOINT = '/api';
  6.   private static $username = '{name}';
  7.   private static $password = '{password}';
  8.   private $protocol;
  9.   private $host_name;
  10.   private $api_uri;
  11.   private $connected = FALSE;
  12.   private $session_cookie;
  13.   private $token;
  14.  
  15.   public function __construct(){
  16.     // Define target
  17.     $this->protocol = PhonyVendorCheck::getTargetProtocol();
  18.     $this->host_name = PhonyVendorCheck::getTargetHostName();
  19.     $this->api_uri = $this->protocol . $this->host_name . ApiConsumer::API_ENDPOINT . '/';
  20.     // Automatically login
  21.     $this->login(self::$username, self::$password);
  22.   }
  23.  
  24.   function __destruct(){
  25.     // Automatically log out
  26.     $this->logout();
  27.   }
  28.  
  29.   /**
  30.    * Determines if the api is connected or not
  31.    */
  32.   public function connected(){
  33.     return $this->connected;
  34.   }
  35.  
  36.   /**
  37.    * Attempts to login a user
  38.    * @param string $username
  39.    * @param string $password
  40.    * @return boolean
  41.    */
  42.   private function login($username, $password){
  43.     if( $this->connected ){
  44.       return TRUE;
  45.     } else {
  46.       $response = $this->request('POST', 'user/login', array(
  47.         'username' => $username,
  48.         'password' => $password,
  49.       ));
  50.       if(
  51.         empty($response)
  52.         || empty($response->session_name)
  53.         || empty($response->sessid)
  54.       ){
  55.         return FALSE;
  56.       }
  57.       $this->connected = TRUE;
  58.       $this->session_cookie = $response->session_name . '=' . $response->sessid;
  59.       $this->token = $response->token;
  60.  
  61.       return TRUE;
  62.     }
  63.   }
  64.  
  65.   /**
  66.    * Attempts to logout a user
  67.    * @param string $username
  68.    * @param string $password
  69.    * @return boolean
  70.    */
  71.   private function logout(){
  72.     if( !$this->connected ){
  73.       return FALSE;
  74.     } else {
  75.       $response = $this->request('POST', 'user/logout');
  76.       if( empty($response) ){
  77.         return FALSE;
  78.       }
  79.       $this->connected = FALSE;
  80.       $this->session_cookie = NULL;
  81.       $this->token = NULL;
  82.       return TRUE;
  83.     }
  84.   }
  85.  
  86.   /**
  87.    * Makes a request
  88.    * @param string $method GET
  89.    * @param string $uri
  90.    * @param array $data
  91.    * @return mixed
  92.    */
  93.   private function request($method = 'GET', $uri, $data = array()){
  94.     //--------------login to the server------------------------
  95.     $service_url = $this->api_uri . $uri . '.json';
  96.     // set up the request
  97.     $curl = curl_init($service_url);
  98.     curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);  // have curl_exec return the string
  99.     if( $method == 'POST' ){
  100.       $json_data = json_encode($data);
  101.       curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
  102.       curl_setopt($curl, CURLOPT_POSTFIELDS, $json_data);
  103.       $headers = array(
  104.         'Content-Type: application/json',
  105.         'Content-Length: ' . strlen($json_data),
  106.       );
  107.       if( !empty($this->token) ){
  108.         $headers[] = 'X-CSRF-Token:' . $this->token;
  109.       }
  110.       curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
  111.     } else {
  112.       $headers = array();
  113.       if( !empty($this->token) ){
  114.         $headers[] = 'X-CSRF-Token:' . $this->token;
  115.       }
  116.       curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
  117.     }
  118.     if( !empty($this->session_cookie) ){
  119.       curl_setopt($curl, CURLOPT_COOKIE, $this->session_cookie);
  120.     }
  121.     // make the request
  122.     curl_setopt($curl, CURLOPT_VERBOSE, true); // output to command line
  123.     try {
  124.       $response = curl_exec($curl);
  125.     } catch(Exception $e) {
  126.       return FALSE;
  127.     }
  128.     curl_close($curl);
  129.     return json_decode($response);
  130.   }
  131. }
Tags: 

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.