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: 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..!

Subscribe For Latest Updates

Sign up to best of PHP Code Solutions, informed analysis and opinions on what matters to you.
Invalid email address
We promise not to spam you. You can unsubscribe at any time.

Bikash

My name is Bikash Kr. Panda. I own and operate PHPCODER.TECH, my native place in Odisha. I am a Web Programmer by profession and working on more than 50 projects to date. My passion is working on the web-based project using PHP and relate to all CMS and frameworks which is based on PHP.

Leave a Reply

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