/    Sign up×
Community /Pin to ProfileBookmark

How To Display On Your Screen/Browser The cURL fetched Page’s Source Code ?

Hi,

I just signed up and this is my first post.
This is my cURL php to fetch a webpage by my web crawler:

[code]
<?php

//ERROR REPORTING CODES.
declare(strict_types=1);
ini_set(‘display_errors’, ‘1’);
ini_set(‘display_startup_errors’, ‘1’);
error_reporting(E_ALL);
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

/*
Download a Webpage via the HTTP GET Protocol using libcurl
*/
function _http ( $target, $referer ) {
//Initialize Handle
$handle = curl_init();
//Define Settings
curl_setopt ( $handle, CURLOPT_HTTPGET, true );
curl_setopt ( $handle, CURLOPT_HEADER, true );
curl_setopt ( $handle, CURLOPT_COOKIEJAR, “cookie_jar.txt” );
curl_setopt ( $handle, CURLOPT_COOKIEFILE, “cookies.txt” );
curl_setopt ( $handle, CURLOPT_USERAGENT, “web-crawler-tutorial-test” );
curl_setopt ( $handle, CURLOPT_URL, $target );
curl_setopt ( $handle, CURLOPT_REFERER, $referer );
curl_setopt ( $handle, CURLOPT_FOLLOWLOCATION, true );
curl_setopt ( $handle, CURLOPT_MAXREDIRS, 4 );
curl_setopt ( $handle, CURLOPT_RETURNTRANSFER, true );
//Execute Request
$output = curl_exec ( $handle );
//Close cURL handle
curl_close ( $handle );
//Separate Header and Body
$separator = “rnrn”;
$header = substr( $output, 0, strpos( $output, $separator ) );
$body_start = strlen( $header ) + strlen( $separator );
$body = substr( $output, $body_start, strlen( $output ) – $body_start );
//Parse Headers
$header_array = Array();
foreach ( explode ( “rn”, $header ) as $i => $line ) {
if($i === 0) {
$header_array[‘http_code’] = $line;
$status_info = explode( ” “, $line );
$header_array[‘status_info’] = $status_info;
} else {
list ( $key, $value ) = explode ( ‘: ‘, $line );
$header_array[$key] = $value;
}
}
//Form Return Structure
$ret = Array(“headers” => $header_array, “body” => $body );
return $ret;
}
$page = _http( “https://potentpages.com”, “” );
$headers = $page[‘headers’];
$http_status_code = $headers[‘http_code’];
$body = $page[‘body’];

echo “$body”;

?>
[/code]

Now, how do I get it to echo the page’s source code on my screen (on the browser) so that I can see the page’s source code. Yes, I know that I can just right click on my browser it pick the “View Source Code” on the menu and view the page’s source code but I do not want to be doing all that manual work for thousands of pages my spider fetches.
Once the cURL fetches the page, I want it to display the page’s source code at the bottom of the page (on my clientside) in a text box so I can easily see the page’ source code once the page has been fetched.
Now, how do I do that ?
Can anyone show me a code sample ?

Thanks!

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@NogDogSep 10.2019 — Not sure if this is what you mean, but maybe change...
<i>
</i>echo $body;

...to...
<i>
</i>echo "&lt;pre&gt;".htmlspecialchars($body)."&lt;/pre&gt;";

...?
Copy linkTweet thisAlerts:
@developer_webauthorSep 10.2019 — Now, how do I get cURL to echo the page's source code on my screen (on the browser) so that I can see the fetched page's source code ?

Yes, I know that I can just right click on my browser for me to pick the "View Source Code" on the menu and view the page's source code that way but I do not want to be doing all that manual work for thousands of pages my spider fetches.

Once the cURL fetches a page, I want it to display that page's source code at the bottom of the page (on my clientside) in a text box so that I can easily see the page' source code once the page has been fetched.

Now, how do I do that ?

Can anyone show me a code sample ?

The best I managed is this ...

I added at the bottom of my code:
<i>
</i>$source_code = htmlspecialchars($body); //Did I get this line correct or not ? Or should I just echo the $body ?
echo "$source_code";

Then I reloaded the page. cURL fetched the page and showed the source code at the bottom of the page. Now, my question is, did I do things correctly or co-incidently I am seeing what I need to see ?

Is this bit of code at the end of my script (see my 1st post for my script) what I should be adding:
<i>
</i>$source_code = htmlspecialchars($body);
echo "$source_code";

If not, then what line of code should I be adding.

Btw, even though I am seeing the fetched page's source code at the bottom of the fetched page, I just see the source code and it's not inside a block text box. How to echo the fetched page's source code in a blocktext ?

In short, I want cURL to fetch the page and then display a blocktext box at the end of the page and that blocktext box should display the fetched page's source code. Something like this following page is doing:

http://johns-jokes.com/downloads/sp-d/johnyboy-curl-test/?url=https://potentpages.com



Thanks!
Copy linkTweet thisAlerts:
@developer_webauthorSep 10.2019 — @NogDog#1608573

Ooops! I missed your post.

Ok, what is the "<pre>" for ?
<i>
</i>echo "&lt;pre&gt;".htmlspecialchars($body)."&lt;/pre&gt;";
Copy linkTweet thisAlerts:
@developer_webauthorSep 10.2019 — @NogDog:

Wow! Your code worked with the <pre> part!

Anyway, how to echo the fetched page's source code inside a blocktext box like this page:

http://johns-jokes.com/downloads/sp-d/johnyboy-curl-test/?url=https://potentpages.com
Copy linkTweet thisAlerts:
@NogDogSep 10.2019 — @developer_web#1608576

That could just be some CSS styling on that &lt;pre&gt; container, maybe setting a max-height, overflow: auto (or whatever the correct value is to get the scrollbar if needed), etc.
×

Success!

Help @developer_web spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 3.29,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,

tipper: Anonymous,
tipped: article
amount: 10 SATS,
)...