Comment implémenter l'affichage de liens externes à l'aide de CURL ?

Barbara Streisand
Libérer: 2024-10-17 21:38:30
original
989 Les gens l'ont consulté

How to Implement External Link Display Using CURL?

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>
Copier après la connexion

When using this code, the following settings are crucial:

  • CURLOPT_AUTOREFERER: Set to TRUE to automatically send the referrer header.
  • CURLOPT_FOLLOWLOCATION: Set to TRUE to automatically follow redirects.

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>
Copier après la connexion

Using these modifications, you can leverage CURL to get and display external links on your website, even if file_get_contents is not supported.

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

source:php
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Derniers articles par auteur
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!