Shan
Shan

Reputation: 2832

Not getting the content of webpage using Curl in Php

I am using a Curl to post some data to a webpage.My question is how to get data of this webpage and store it in a file?

 <?
 extract($_POST);
 //set POST variables
 $trainno="12635";
 $url = 'http://www.indianrail.gov.in/cgi_bin/inet_trnnum_cgi.cgi';
 $fields = array(
                    'lccp_trnname'=>urlencode($trainno),
           );

 //url-ify the data for the POST
 foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
 rtrim($fields_string,'&');

 //open connection
 $ch = curl_init();

 //set the url, number of POST vars, POST data
 curl_setopt($ch,CURLOPT_URL,$url);
 curl_setopt($ch,CURLOPT_POST,count($fields));
 curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
 curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');

 //execute post
 $result = curl_exec($ch);

 //close connection
 curl_close($ch);
 echo $result;
?>

Upvotes: 0

Views: 378

Answers (2)

Michael Hodgins
Michael Hodgins

Reputation: 35

You need to look at PHP's DOMDocument::loadHTML() method (http://docs.php.net/manual/en/domdocument.loadhtml.php). When you're building your querystring, you don't need to do this manually; you should use http_build_query() (http://www.php.net/manual/en/function.http-build-query.php).

Upvotes: 0

Captain Payalytic
Captain Payalytic

Reputation: 1071

Your question is not clear. Are you saying that you are getting nothing returned from the site call, or simply that it is not getting put into $result?

To get data returned by curl into $result, you need to set:

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 

Upvotes: 1

Related Questions