WebTechKitchen; Your Web Technology Kitchen, contact us to create, or maintain your websites and other digital properties.

Drupal 7 connect to Drupal with curl and use rest services

Submitted by barnettech on Tue, 05/10/2011 - 15:16
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://base_drupal_url/drupal7/user/login");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_USERAGENT, 'PHP script');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, 
"name=username&pass=password&form_id=user_login");
ob_start();  //dont print out this output
curl_exec ($ch);

curl_setopt($ch, CURLOPT_URL,"http://base_drupal_url/drupal7/test_endpoint/users");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json'));
curl_setopt($ch, CURLOPT_HTTPGET, 1);
ob_end_clean();  //resume printout output to screen

$ret = new stdClass;
$ret->response = curl_exec($ch); // execute and get response
$ret->error    = curl_error($ch);
$ret->info     = curl_getinfo($ch);

/*if ($ret->info['http_code'] == 200) {
      $ret->response = json_decode($ret->response);
    }*/

//print_r($ret);
curl_close ($ch);
unset($ch);



?>