How To Use PHP to Curl a Web Page

To use PHP to curl a web page, you can use the built-in curl function in PHP. The curl function allows you to send HTTP requests and receive responses using the curl library.

Here’s an example of how to use PHP to curl a web page:

<?php
// Create a new curl resource
$ch = curl_init();
$user_agent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3';

// Set the URL and other options
curl_setopt($ch, CURLOPT_URL, "https://www.example.com");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);

// Send the request and get the response
$response = curl_exec($ch);

// Close the curl resource
curl_close($ch);

// Output the response
echo $response;
 ?>

In this example, we’re sending a GET request to the https://www.example.com URL using the curl function. The CURLOPT_RETURNTRANSFER option is set to 1 to return the response instead of outputting it directly. The response is stored in the $response variable and outputted using the echo function.

You can also use curl to send POST requests by setting the CURLOPT_POST option to 1 and passing the POST data using the CURLOPT_POSTFIELDS option.

Leave a Reply

Proudly powered by WordPress | Theme: Code Blog by Crimson Themes.