0

我正在使用 Curl 将一些数据发布到网页。我的问题是如何获取该网页的数据并将其存储在文件中?

 <?
 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;
?>
4

2 回答 2

1

你的问题不清楚。您是说站点调用没有返回任何内容,或者只是没有将其放入 $result 中?

要将 curl 返回的数据获取到 $result 中,您需要设置:

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
于 2013-03-07T15:22:02.807 回答
0

您需要查看 PHP 的 DOMDocument::loadHTML() 方法(http://docs.php.net/manual/en/domdocument.loadhtml.php)。当您构建查询字符串时,您不需要手动执行此操作;你应该使用 http_build_query() ( http://www.php.net/manual/en/function.http-build-query.php )。

于 2013-03-07T15:25:59.040 回答