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.
$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;
Comments
mycurl_curl() should return a
mycurl_curl() should return a render array or text, I suspect it should be returning $content instead of $header.
I tried replacing $content
I tried replacing $content with $header but still getting same result.
You can make requests to
You can make requests to external servers with drupal_http_request().
Contact me to contract me for D7 -> D10/11 migrations.
Now content is not displaying
Now content is not displaying only menu title i.e "My curl demo" is appearing on the page and not the content.
What does your code look like
What does your code look like?
It's same code as above
Its a same code as above submitted by @conongchamch
Why don't you just use drupal
Why don't you just use drupal_http_request()? It handles all this for you (it uses curl in the background).
Contact me to contract me for D7 -> D10/11 migrations.
It is working now
Thanks a lot Jaypan for your drupal_http_request() method suggestion it is working fine now.