Replacing file_get_contents with CURL for External Link Display
In certain scenarios, the file_get_contents function may not be available, necessitating the use of an alternative such as CURL. This article addresses the issue of getting and displaying external links using CURL.
To implement CURL, follow these steps:
<code class="php">function file_get_contents_curl($url) { $ch = curl_init(); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_URL, $url); $data = curl_exec($ch); curl_close($ch); return $data; }</code>
When using this code, the following settings are crucial:
By enabling these options, CURL can effectively access external links and retrieve their content. For instance, the following code retrieves the contents of Google.com:
<code class="php">echo file_get_contents_curl('http://google.com');</code>
Using these modifications, you can leverage CURL to get and display external links on your website, even if file_get_contents is not supported.
The above is the detailed content of How to Implement External Link Display Using CURL?. For more information, please follow other related articles on the PHP Chinese website!