簡體   English   中英

在PHP中使用Curl無法獲取網頁內容

[英]Not getting the content of webpage using Curl in Php

我正在使用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;
?>

您的問題不清楚。 您是在說從站點呼叫中沒有得到任何回報,還是只是沒有將其放入$ result中?

要將curl返回的數據獲取為$ result,您需要設置:

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 

您需要查看PHP的DOMDocument :: loadHTML()方法( http://docs.php.net/manual/en/domdocument.loadhtml.php )。 建立查詢字串時,您不需要手動進行; 您應該使用http_build_query()( http://www.php.net/manual/zh/function.http-build-query.php )。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM