Hi all, I want to get the source code of a page in PHP. There is a page, lets say google.com - I want to get the output HTML source and for PHP to read it, so I can find a string of code in the source. How do I do this? I am using cURL to get the page and I can display the page... but I don't know how to get the source code. Please help! Thanks.
I'm not familiar with cURL, but you can do something like this in file_get_contents: <?php header('Content-Disposition: attachment; filename="lol.txt"'); // 'lol.txt' contains html source echo file_get_contents("http://google.com/"); ?> PHP:
$code = file_get_contents('http://google.com'); PHP: And then you'd just have to do a preg_match() on $code.
$file = fopen("file.txt", "w"); $ch = curl_init("http://site.com/page.php"); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER,true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_FILE, $file); curl_exec($ch); curl_close($ch); fclose($file); PHP:
<?php $ch = curl_init("http://location.us"); // Start cURL session curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // No output to client, allows storing content in a string instead $resource = curl_exec($ch); // Execure cURL session and store contents as a variable curl_close($ch); // Close cURL session echo $resource; // Send contents to user preg_match("!Google!",$resource,$match); // Use contents in a regex and so on ?> PHP:
Hi, I can do that already - I can already get the code and display it. However, I want the HTML source - for example, I can get "Hello my name is bob!" from the page, but I want to get the HTML code: <strong>Hello my name is bob!</strong> - do you see?
Hey, I sorted it now - I had tried preg_match to get something but it wouldn't work because the code coming back was not what I expected. All good now though, my mistake! Thanks though that would've worked.
n HTML a comment's main purpose is to serve as a note to you, the web developer or to others who may view your website's source code. However, PHP's comments are different in that they will not be displayed to your visitors. The only way to view PHP comments is to open the PHP file for editing. This makes PHP comments only useful to PHP programmers.