如何在外部项目中使用Wordpress的http.php?

时间:2010-03-22 15:31:10

标签: php text-parsing file-access

答案:使用Curl实现......

$file = "http://abc.com/data//output.txt";
$ch = curl_init($file);
$fp = @fopen("out.txt", "w");
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
$file = "out.txt";
$fp = fopen($file, "r");

我正在尝试解析托管在另一台服务器上的管道分隔文本文件中的数据,该文件又将插入数据库中。我的主机(1和1)在 php.ini 中禁用了 allow_url_fopen

错误讯息:

Warning: fopen() [function.fopen]: URL file-access is disabled in the server configuration in

代码:

    <?


// make sure curl is installed
if (function_exists('curl_init')) {
   // initialize a new curl resource
   $ch = curl_init(); 

   // set the url to fetch
   curl_setopt($ch, CURLOPT_URL, 'http://abc.com/data/output.txt'); 

   // don't give me the headers just the content
   curl_setopt($ch, CURLOPT_HEADER, 0); 

   // return the value instead of printing the response to browser
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 

   // use a user agent to mimic a browser
   curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.5) Gecko/20041107 Firefox/1.0'); 

   $content = curl_exec($ch); 

   // remember to always close the session and free all resources 
   curl_close($ch); 
} else {
   // curl library is not installed so we better use something else
}

//$contents = fread ($fd,filesize ($filename));

//fclose ($fd); 
$delimiter = "|";
$splitcontents = explode($delimiter, $contents);
$counter = "";
?>
<font color="blue" face="arial" size="4">Complete File Contents</font>
<hr>
<?
echo $contents;
?>

<br><br>
<font color="blue" face="arial" size="4">Split File Contents</font>
<hr>
<?
foreach ( $splitcontents as $color )
{

$counter = $counter+1;
echo "<b>Split $counter: </b> $colorn<br>";
}

?>

Wordpress有这个很酷的http.php文件。有没有更好的方法呢?如果没有,我如何使用http.php执行此任务?谢谢你们..

1 个答案:

答案 0 :(得分:1)

尝试使用file_get_contents()或CURL库。

本文有一些示例:Reading a Remote File Using PHP