473,394 Members | 1,742 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Custom HTTP request not returning expected results.

Please can somebody tell me why this isn't returning the index page? It is technically supposed to, and if somebody can get it to work I can then use remote download resume functionality in my site. According to <link removed>, it is a HTTP 1.1 request that supposedly should work...I can't find any syntax errors! What's wrong?

[php]<?php
ob_end_clean();
ob_implicit_flush(TRUE);

$host = "www.example.com";
$url = "/index.html";
$port = 80;
$saveToFile = "index.html";
$nn = "\r\n";
$method = "GET";

$fp = @fsockopen($proxyHost ? $scheme.$proxyHost : $scheme.$host, $proxyPort ? $proxyPort : $port, $errno, $errstr, 15);

if (!$fp)
{
html_error("Couldn't connect to ".($proxyHost ? $proxyHost : $host)." at port ".($proxyPort ? $proxyPort : $port), 0);
}

if($errno || $errstr)
{
$lastError = $errstr;
print $errstr."<br>".$errno."<br>";
}

socket_set_timeout($fp, 120);

if ($saveToFile)
{
if ($proxy)
{
echo "<p>Connected to proxy: <b>".$proxyHost."</b> at port <b>".$proxyPort."</b>...<br>\n";
echo "GET: <b>".$host.$url."</b>...<br>\n";
}
else
{
echo "<p>Connected to: <b>".$host."</b> at port <b>".$port."</b>...<br>";
}
}

if ($saveToFile)
{
do {
$redirect = "";

$request =
"HEAD ".str_replace(" ", "%20", $url)." HTTP/1.1".$nn.
"Host: ".$host.$nn.
"User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; MEGAUPLOAD 1.0; Alexa Toolbar)".$nn.
"Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5".$nn.
"Accept-Language: en-en,en;q=0.5".$nn.
"Accept-Charset: windows-1251,utf-8;q=0.7,*;q=0.7".$nn.
"Pragma: no-cache".$nn.
"Cache-Control: no-cache".$nn.
"Keep-Alive: 300".$nn.
"Connection: Keep-Alive".$nn.$nn;

fwrite($fp, $request);

//$fs = fopen("debug.txt", "wb");
//fwrite($fs, $request);

$head = "";

while(!feof($fp))
{
$head.= fgets($fp, 128);
}

print "<br>HEAD: ".$head.$nn;

if (stristr($head, "Location:"))
{
$redirect = trim(cut_str($head, "Location:", "\n"));
$rUrl = parse_url($redirect);
$scheme = $rUrl["scheme"] == "https" ? "ssl://" : "";
$port = $rUrl["port"] ? $rUrl["port"] : ($scheme == "ssl://" ? 443 : 80);
$host = $rUrl["host"];
$url = $rUrl["path"].($rUrl["query"] ? "?".$rUrl["query"] : "");
}

if (stristr($head, "Content-Disposition:"))
{
$Content = trim(cut_str($head, "Content-Disposition:", "\n"))."\n";
if (stristr($Content, "filename="))
{
$FileName = trim(trim(trim(cut_str($Content, "filename=", "\n")), ";"), '"');
$saveToFile = dirname($saveToFile).PATH_SPLITTER.basename($FileN ame);
}
}
/*
if (@file_exists($saveToFile))
{
if (is_numeric($filesize = @filesize($saveToFile)))
{
$Resume["use"] = TRUE;
$Resume["from"] = intval($filesize);
}
}
*/
} while (!empty($redirect));
}



$request =
$method." ".str_replace(" ", "%20", $url)." HTTP/1.1".$nn.
"Host: ".$host.$nn.
"User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; MEGAUPLOAD 1.0; Alexa Toolbar)".$nn.
"Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5".$nn.
"Accept-Language: en-en,en;q=0.5".$nn.
"Accept-Charset: windows-1251,utf-8;q=0.7,*;q=0.7".$nn.
"Pragma: no-cache".$nn.
"Cache-Control: no-cache".$nn.
($Resume["use"] === TRUE ? "Range: bytes=".$Resume["from"]."-".$nn : "").
$http_auth.
$proxyauth.
$referer.
$cookies.
"Keep-Alive: 300".$nn.
"Connection: Keep-Alive".$nn.
$content_tl.$nn.$postdata;

//fwrite($fs, $request);
//fclose($fs);

fwrite($fp, $request);


while(!feof($fp))
{
$data.= fgets($fp, 128);
}
print "<br>GET: ".$data.$nn;

fclose($fp);


function cut_str($str, $left, $right)
{
$str = substr(stristr($str, $left), strlen($left));
$leftLen = strlen(stristr($str, $right));
$leftLen = $leftLen ? -($leftLen) : strlen($str);
$str = substr($str, 0, $leftLen);
return $str;
}
?>[/php]
Nov 1 '07 #1
1 1543
Atli
5,058 Expert 4TB
Hi. Welcome to TSDN!

I've edited out some links in your post, as most links to sites outside TSDN are not allowed in the technical forums.

I've also moved your post into it's own thread in the PHP Forums. It is generally a very good rule to always post new questions in new threads, to avoid hijacking the old thread and to make it easier for other people facing similar problems to find your questions and any answers it may have received.

Check out the Posting Guidelines for more info on our posting policies.

Moderator
Nov 2 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Alex Stevens | last post by:
I'd already posted this in microsoft.public.dotnet.framework.windowsforms and microsoft.public.dotnet.framework.windowsforms.controls to no avail so apologies for the cross-posting. Hi, I'm...
0
by: Santa | last post by:
I am using Fritz Onion's "Asynchronous Pages" approach as mentioned in the article http://msdn.microsoft.com/msdnmag/issues/03/06/Threading/default.aspx to increase the performance of my ASPX...
0
by: psy000 | last post by:
Hi, I have a C# web service client that talks to a JAVA application sever. I use AXIS to generate the WSDL file, use wsdl.exe to generate proxy stub c# code. When I try to use c# client connect...
10
by: William L. Bahn | last post by:
I'm looking for a few kinds of feedback here. First, there is a program at the end of this post that has a function kgets() that I would like any feedback on - including style. Second, for...
2
by: Matt | last post by:
Hello all, The app we are working on uses custom errors extensively to provide friendly error pages to users whilst logging the actual exceptions behind the scenes. However.... We are now...
1
by: Elie Medeiros via .NET 247 | last post by:
Hi there, I have written a custom validator function to validate a datefrom a user-filled field. The function tries to parse the dateand if it can't, sets (serverValidateEventArgs)e.IsValid...
4
by: hellrazor | last post by:
Hi there, I'm trying to consume a web-service that is supposed to return a collection of a custom object. The web-service was not created with C# or VS.net. It was created with IBM VisualAge...
8
by: Tomasz | last post by:
Hello Developers! I have an interesting problem using my custom MembershipProvider, RoleProvider and Forms Authentication. Both MembershipProvider and RoleProvider require session state, where...
0
dmjpro
by: dmjpro | last post by:
I am calling two methods having request object. In two method I am retrieving ServletInputStream using request.getInputStream. For first method call it's working fine but for second method call...
0
by: Jordan S. | last post by:
Using .NET 3.5... in a "plain old" .aspx page I have the following code in the Init event: this.Context.Response.Cache.SetExpires(DateTime.Now.AddSeconds(60));...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.