Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting at headers when requesting a URL 1

Status
Not open for further replies.

matthewralston

Programmer
Apr 30, 2001
12
GB
I want to fetch a URL in PHP with fopen or popen or something.
Getting the main content of the page is fine, but I want to access the headers that the server sends back with the page. I'm sure it can be done with popen (or some command to open a tcp/ip connection to port 80?)
 
try this:

$fp = fsockopen($host, 80, &$errno, &$errstr, 120);
$ua = $_SERVER["HTTP_USER_AGENT"];
if (!$fp) {
print &quot;error: $errstr ($errno)<BR>\n&quot;;
} else {
if ($method == &quot;GET&quot;) {
fputs($fp, &quot;GET $usepath HTTP/1.0\n&quot;);
} else if( $method == &quot;POST&quot; ) {
fputs($fp, &quot;POST $usepath HTTP/1.0\n&quot;);
};
fputs($fp, &quot;User-Agent: &quot;.$ua.&quot;\n&quot;);
fputs($fp, &quot;Accept: */*\n&quot;);
fputs($fp, &quot;Accept: image/gif\n&quot;);
fputs($fp, &quot;Accept: image/x-xbitmap\n&quot;);
fputs($fp, &quot;Accept: image/jpeg\n&quot;);
if ($method == &quot;POST&quot;) {
$strlength = strlen($postdata);
fputs($fp, &quot;Content-type: application/x- fputs($fp, &quot;Content-length: &quot;.$strlength.&quot;\n\n&quot;);
fputs($fp, $postdata.&quot;\n&quot;);
};
fputs($fp, &quot;\n&quot;);
$output = &quot;&quot;;
while (!feof($fp)) {
$output .= fgets($fp, 1024);
};
fclose($fp);
};

output contains headers + html
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top