0

In this script curl is working fine. $output has the page but when I try to echo its source code it displays nothing. I think it has something to do with page headers. Any help!!

header('content-type:text/plain');
$ch = curl_init();
$url = "https://www.amazon.com/gp/css/shiptrack/view.html/ref=TE_SIMP_typ?ie=UTF8&addressID=olqnkqjstjp&latestArrivalDate=1483848000&orderID=103-0753412-5410653&shipmentDate=1483706902&orderingShipmentId=4160817392100&packageId=1";
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_HEADER, false);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_BINARYTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 1);
$output = curl_exec($ch);
curl_close($ch);
echo $output;
5
  • Why downvote .What's not clear. Commented Jan 10, 2017 at 7:31
  • when debugging, use var_dump , not echo. var_dump is guaranteed to give some output, what does var_dump say? (most likely, it says bool(false) , meaning that curl had an error, in which case, use curl_error($ch) to get more info) Commented Jan 10, 2017 at 14:21
  • seems you're running without error reporting, and you reference an undefined variable called $curl (because the variable you want is called $ch , $curl is undefined), the script crashes, but without error_reporting in php.ini , you just get a blank page and no error log - make sure php.ini contains error_reporting=E_ALL, and run the code again and the error should be obvious from the error log Commented Jan 10, 2017 at 14:26
  • @Bilal's edited answer has worked for me Commented Jan 11, 2017 at 8:35
  • @rajeev singh can pls accept my answer by clicking on that tick mark near votes...so it will be helpful to others too(When u accept it will turn into green tick) Commented Jan 16, 2017 at 12:15

1 Answer 1

1

I have modified your code.. Try this.

Remove
header('content-type:text/plain');

and replace your code with mine.

$ch = curl_init();
$url = "https://www.amazon.com/gp/css/shiptrack/view.html/ref=TE_SIMP_typ?ie=UTF8&addressID=olqnkqjstjp&latestArrivalDate=1483848000&orderID=103-0753412-5410653&shipmentDate=1483706902&orderingShipmentId=4160817392100&packageId=1";
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_HEADER, false);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 1);
$output = curl_exec($ch);
curl_close($ch);
echo $output;

EDIT

Since you want ctrl+u

Here is the PHP code to fetch the html source code of any website specified. fopen function is used to open the website URL. stream_get_contents is used to read the opened URL. The fetched code is displayed inside a textarea.

$domain = 'https://www.amazon.com/gp/css/shiptrack/view.html/ref=TE_SIMP_typ?ie=UTF8&addressID=olqnkqjstjp&latestArrivalDate=1483848000&orderID=103-0753412-5410653&shipmentDate=1483706902&orderingShipmentId=4160817392100&packageId=1';
$handle = fopen($domain, 'r');
$content = stream_get_contents($handle);
/**
// alternative to stream_get_contents
$contents = '';
while (!feof($handle)) {
    $content .= fread($handle, 8192);
}
**/ 
fclose($handle);
/** 
 * print html content on textarea
 */
echo "<textarea rows='20' cols='80'>$content</textarea>";

output will be like this: enter image description here

OR

Also if you want to manipulate the retrieved page somehow, you might want to try some php DOM parser. I find PHP Simple HTML DOM Parser very easy to use.

Sign up to request clarification or add additional context in comments.

10 Comments

$output has the page but I want to get its html, that is where I am facing problem.
Rajeev singh i didn't get u.. can u elaborate?? i thought you had problem with ur curl
I want the html of the page so I can scrap some content from it.
@bilal. sorry I was not clear b4. my curl is workinf fine and the $output contains the page that I want but I want that page html so I acn scrap some content from it.
what exactly are u trying to do?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.