donljt2606 2010-03-22 15:31
浏览 27
已采纳

如何在外部项目中使用Wordpress的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..

  • 写回答

1条回答 默认 最新

  • doutu2017 2010-03-22 15:36
    关注

    Try file_get_contents(), or the CURL library.

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)