Search code examples
phpcurldrupaldrupal-7

To develop custom curl functionality in drupal7


I am newbie to drupal and trying to develop a custom module for curl functionality of php which will get the content of page and will display the whole content as it is.I tried but it is not displaying the content below is the code that I have tried.

function mycurl_menu() {

 $items['mycurl'] = array(
     'title' => 'My curl demo',
     'page callback' => 'mycurl_curl',
     'access callback' => TRUE,
     'type' => MENU_CALLBACK,
 );
 return $items;
}

function mycurl_curl() {

 $user_agent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.65 Safari/537.36";
 $postdata ="name=amit&pass=amit";
 $options = array(

     CURLOPT_CUSTOMREQUEST  =>"GET",        //set request type post or get
     CURLOPT_POST           =>false,        //set to GET
     CURLOPT_USERAGENT      => $user_agent, //set user agent
     CURLOPT_COOKIEFILE     =>"cookie.txt", //set cookie file
     CURLOPT_COOKIEJAR      =>"cookie.txt", //set cookie jar
     CURLOPT_RETURNTRANSFER => true,     // return web page
     CURLOPT_HEADER         => false,    // don't return headers
     CURLOPT_FOLLOWLOCATION => true,     // follow redirects
     CURLOPT_ENCODING       => "",       // handle all encodings
     CURLOPT_AUTOREFERER    => true,     // set referer on redirect
     CURLOPT_CONNECTTIMEOUT => 120,      // timeout on connect
     CURLOPT_TIMEOUT        => 120,      // timeout on response
     CURLOPT_MAXREDIRS      => 10,       // stop after 10 redirects
 );

 $url = "http://localhost/drupal_commerce/product";

 $ch      = curl_init( $url );
 curl_setopt_array( $ch, $options );
 $content = curl_exec( $ch );
 $err     = curl_errno( $ch );
 $errmsg  = curl_error( $ch );
 $header  = curl_getinfo( $ch );
 curl_close( $ch );

 $header['errno']   = $err;
 $header['errmsg']  = $errmsg;
 $header['content'] = $content;
 return $header;

}

Solution

  • You should return $header['content'] since the menu callback is no supposed to return an array, and check the permissions of the page you are fetching with curl so that is it available.