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

发布于 2024-08-26 06:47:26 字数 2037 浏览 7 评论 0原文

答案:使用 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");

我正在尝试从托管在另一台服务器上的管道分隔文本文件中解析数据,该文件又将被插入到数据库中。我猜我的主机(1and1)在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 来完成此任务?谢谢各位..

Answer : Implemented using 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");

I am trying to parse data from a pipe-delimited text file hosted on another server which in turn will be inserted in a database. My host (1and1) disabled allow_url_fopen in php.ini I guess.

Error message :

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

Code :

    <?


// 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 has this cool http.php file. Is there a better way of doing it? If not, how do I use http.php for this task? Thank you guys..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北音执念 2024-09-02 06:47:26

尝试 file_get_contents() 或 CURL 库。

本文有一些示例: 使用 PHP 读取远程文件

Try file_get_contents(), or the CURL library.

This article has some examples: Reading a Remote File Using PHP

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文