cURL in plaats van file_get_contents() en file()
Inhoud
1. Alternatief voor file_get_contents()
2. Alternatief voor fopen()
3. Alles in één eenvoudige classe
4. Zie ook
1. Alternatief voor file_get_contents()
In Plaats Van:
Code (php)
1
2
3
4
5
6
2
3
4
5
6
<?php
$file_contents = file_get_contents('http://example.com/');
// display file
echo $file_contents;
?>
$file_contents = file_get_contents('http://example.com/');
// display file
echo $file_contents;
?>
Gebruik dit:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
<?php
$ch = curl_init();
$timeout = 5; // set to zero for no timeout
curl_setopt ($ch, CURLOPT_URL, 'http://example.com');
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$file_contents = curl_exec($ch);
curl_close($ch);
// display file
echo $file_contents;
?>
$ch = curl_init();
$timeout = 5; // set to zero for no timeout
curl_setopt ($ch, CURLOPT_URL, 'http://example.com');
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$file_contents = curl_exec($ch);
curl_close($ch);
// display file
echo $file_contents;
?>
2. Alternatief voor file()
In Plaats Van:
Code (php)
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
<?php
$lines = file('http://example.com/');
// display file line by line
foreach($lines as $line_num => $line) {
echo "Line # {$line_num} : ".htmlspecialchars($line)."<br />\n";
}
?>
$lines = file('http://example.com/');
// display file line by line
foreach($lines as $line_num => $line) {
echo "Line # {$line_num} : ".htmlspecialchars($line)."<br />\n";
}
?>
Gebruik dit:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?php
$ch = curl_init();
$timeout = 5; // set to zero for no timeout
curl_setopt ($ch, CURLOPT_URL, 'http://example.com');
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$file_contents = curl_exec($ch);
curl_close($ch);
$lines = array();
$lines = explode("\n", $file_contents);
// display file line by line
foreach($lines as $line_num => $line) {
echo "Line # {$line_num} : ".htmlspecialchars($line)."<br />\n";
}
?>
$ch = curl_init();
$timeout = 5; // set to zero for no timeout
curl_setopt ($ch, CURLOPT_URL, 'http://example.com');
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$file_contents = curl_exec($ch);
curl_close($ch);
$lines = array();
$lines = explode("\n", $file_contents);
// display file line by line
foreach($lines as $line_num => $line) {
echo "Line # {$line_num} : ".htmlspecialchars($line)."<br />\n";
}
?>
3. Alles in één eenvoudige classe
Gebruik de volgende classe om het lezen/opslaan van externe bestanden te vereenvoudigen. deze Classe zal automatisch de tijdelijke bestanden die op het eind van uw PHP script gedownload worden verwijderen.
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?php
class downloader {
var $tempFolder;
var $tempFiles = array();
function __destruct () {
foreach ($this->tempFiles as $file) {
unlink($file['temp']);
}
}
function __construct ($temp)
{
$this->tempFolder = $temp;
}
function get ($url) {
array_unshift($this->tempFiles, array(
'extension'=> array_pop(explode('.', $url)),
'original'=> basename($url),
'temp'=> $this->tempFolder . md5(microtime()),
));
$ch = curl_init($url);
$fp = fopen($this->tempFiles[0]['temp'], 'w');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_exec($ch);
curl_close($ch);
fclose($fp);
return $this->tempFiles[0]['temp'];
}
function read ($index = 0) {
return file_get_contents($this->tempFiles[$index]['temp']);
}
function readArray ($index = 0)
{
return file($this->tempFiles[$index]['temp']);
}
function listFiles () {
return $this->tempFiles;
}
function save ($path, $index = 0) {
copy($this->tempFiles[$index]['temp'], (is_dir($path) ? $path . $this->tempFiles[$index]['original'] : $path));
}
}
$d = new downloader('/home/<username>/<temp folder>');
?>
class downloader {
var $tempFolder;
var $tempFiles = array();
function __destruct () {
foreach ($this->tempFiles as $file) {
unlink($file['temp']);
}
}
function __construct ($temp)
{
$this->tempFolder = $temp;
}
function get ($url) {
array_unshift($this->tempFiles, array(
'extension'=> array_pop(explode('.', $url)),
'original'=> basename($url),
'temp'=> $this->tempFolder . md5(microtime()),
));
$ch = curl_init($url);
$fp = fopen($this->tempFiles[0]['temp'], 'w');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_exec($ch);
curl_close($ch);
fclose($fp);
return $this->tempFiles[0]['temp'];
}
function read ($index = 0) {
return file_get_contents($this->tempFiles[$index]['temp']);
}
function readArray ($index = 0)
{
return file($this->tempFiles[$index]['temp']);
}
function listFiles () {
return $this->tempFiles;
}
function save ($path, $index = 0) {
copy($this->tempFiles[$index]['temp'], (is_dir($path) ? $path . $this->tempFiles[$index]['original'] : $path));
}
}
$d = new downloader('/home/<username>/<temp folder>');
?>
4. Zie Ook
allow_url_fopen