How to Open an External URL in PHP

To open an external URL in PHP, we will use cURL which is a PHP library, a 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

We will take 2 steps to do 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 the success code

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

To know more about the 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..!

Was this article helpful?
YesNo
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 189

One thought on “How to Open an External URL in PHP

Leave a Reply

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

Related Posts

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

Back To Top