How to use Wordpress' http.php in external projects?
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.开发者_Python百科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..
Try file_get_contents()
, or the CURL library.
This article has some examples: Reading a Remote File Using PHP
精彩评论