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.

How to open an external URL in PHP

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:

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

Also Check:

Happy Coding..!

Was this article helpful?
My name is Bikash Kr. Panda. I own and operate PHPCODER.TECH. I am a web Programmer by profession and working on more than 50 projects to date. Currently I am working on the web-based project and all the CMS and frameworks which are based on PHP.
Posts created 188

Leave a Reply

Your email address will not be published.

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top