How can I get the html source code of http://www.example-webpage.com/file.html
without using file_get_contents()
?
I need to know this because on some webhosts allow_url_fopen
is disabled so you can't use file_get_contents()
. Is it possible to get the html file's source with cURL (if cURL support is enabled)? If so, how?
Thanks.
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
curl_close($curl);
Source: http://www.christianschenk.org/blog/php-curl-allow-url-fopen/
Try http://php.net/manual/en/curl.examples-basic.php :)
<?php
$ch = curl_init("http://www.example.com/");
$fp = fopen("example_homepage.txt", "w");
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
$output = curl_exec($ch);
curl_close($ch);
fclose($fp);
?>
As the documentation says:
The basic idea behind the cURL functions is that you initialize a cURL session using the curl_init(), then you can set all your options for the transfer via the curl_setopt(), then you can execute the session with the curl_exec() and then you finish off your session using the curl_close().
I found a tool in Github that could possibly be a solution to this question. https://incarnate.github.io/curl-to-php/ I hope that will be useful
Source: Stackoverflow.com