Example PHP Code to Curl a Web Page and Display the 9th HTML Table from the Web Page

Here’s an example PHP code that uses cURL to fetch a web page and display the 9th table from the page:

// Set the URL of the web page
$url = "https://www.example.com";

// Initialize a cURL session
$ch = curl_init($url);

// Set cURL options
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

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

// Close the cURL session

// Parse the HTML response using PHP DOM
$dom = new DOMDocument();

// Find the 9th table element in the HTML response
$tables = $dom->getElementsByTagName('table');
$target_table = $tables->item(8);

// Display the HTML content of the 9th table
echo $dom->saveHTML($target_table);

In this example, we’re using the curl_init function to initialize a cURL session with the URL of the web page we want to fetch. Then we’re using the curl_setopt function to set the CURLOPT_RETURNTRANSFER option to true, which tells cURL to return the response instead of outputting it directly.

Next, we’re using the curl_exec function to execute the cURL request and get the response. After that, we’re using PHP’s DOMDocument class to parse the HTML response and find the 9th table element using getElementsByTagName.

Finally, we’re using the saveHTML method of the DOMDocument object to get the HTML content of the 9th table element and display it using the echo function.

Leave a Reply

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