Home » How to Open an External URL in PHP

How to Open an External URL in PHP

To open an external URL in PHP, we use cURL which is a PHP library. cURL is also a command-line tool that is used to send files and download data over HTTP and FTP.

Check live view below.

On this, we takes 2 steps to doing this.

  • Create an HTML form with only a button with a name attribute.
  • Create a PHP cURL function that is used to open an external URL.

Create HTML Form

This form is only used for clicking the button to open an external URL.

Create PHP Script to Open External URL

Code Explanations:

  • $ch = curl_init();
    • Create a new cURL resource
  • curl_setopt($ch, CURLOPT_URL, $url);
    • Set the file URL to fetch through cURL
  • curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    • Do not check the SSL certificates
  • curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    • Return the actual result of the curl result instead of success code

At the last we call the function on the time of button click.

To know more about PHP cURL library you check here: https://www.php.net/manual/en/book.curl.php

Here is the live view of the complete source code to How we open the external URL.

Also Check:

Happy Coding..!

Leave a Reply

Your email address will not be published. Required fields are marked *