How do I get the HTML code of a web page in PHP?

0 votes

I want to retrieve the HTML code of a link (web page) in PHP. For example, if the link is

https://edureka.co/

then I want the HTML code of the page which is served. I want to retrieve this HTML code and store it in a PHP variable.

How can I do this?

Oct 6 in PHP by kartik
• 36,020 points
77 views

1 answer to this question.

0 votes

Hello @kartik,

If your PHP server allows url fopen wrappers then the simplest way is:

$html = file_get_contents('https://edureka.co/');

If you need more control then you should look at the cURL functions:

$c = curl_init('https://edureka.co/');
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
//curl_setopt(... other options you want...)

$html = curl_exec($c);

if (curl_error($c))
    die(curl_error($c));

// Get the status code
$status = curl_getinfo($c, CURLINFO_HTTP_CODE);

curl_close($c);

Hope it helps!!
Thank you!!

answered Oct 6 by Niroj
• 78,480 points

Related Questions In PHP

0 votes
1 answer

How can I get the classname from a static call in an extended PHP class?

Hello @kartik, __CLASS__ always returns the name of the ...READ MORE

answered Oct 27 in PHP by Niroj
• 78,480 points
30 views
0 votes
1 answer

How to get the list of specific files in a directory using php?

Hello @kartik, You'll be wanting to use glob() Example: $files = ...READ MORE

answered Nov 6 in PHP by Niroj
• 78,480 points
26 views
0 votes
1 answer

How do I find the mime-type of a file with php?

Hello @kartik, mime_content_type() is deprecated, so you won't ...READ MORE

answered Nov 16 in PHP by Niroj
• 78,480 points
16 views
0 votes
1 answer

How can I handle the warning of file_get_contents() function in PHP?

Hello @kartik, This is fairly simple: if (!$data = ...READ MORE

answered Apr 7 in PHP by Niroj
• 78,480 points
1,555 views
+1 vote
1 answer

How to make anchor tag with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18 in Laravel by Niroj
• 78,480 points
3,528 views
0 votes
1 answer

What is redirection in Laravel?

Named route is used to give specific ...READ MORE

answered Mar 18 in Laravel by Niroj
• 78,480 points
421 views
0 votes
1 answer

How to install Laravel via composer?

Hello, This is simple you just need to ...READ MORE

answered Mar 23 in Laravel by Niroj
• 78,480 points
292 views
+1 vote
1 answer

What are named routes in Laravel and How can specify route names for controller actions?

Hey @kartik, Named routing is another amazing feature of ...READ MORE

answered Mar 23 in Laravel by Niroj
• 78,480 points
4,230 views
0 votes
1 answer

How do I get the last inserted ID of a MySQL table in PHP?

Hello @kartik, If you're using PDO, use PDO::lastInsertId. If you're ...READ MORE

answered Oct 22 in PHP by Niroj
• 78,480 points
108 views
0 votes
1 answer

How do I get a file extension in PHP?

Hello @kartik, pathinfo() $path_info = pathinfo('/foo/bar/baz.bill'); echo $path_info['extension']; // "bill" Hope ...READ MORE

answered Sep 16 in PHP by Niroj
• 78,480 points
68 views