Jump to content

Why is not downloading the remote file?


LLeoun

Recommended Posts

Hi all,

 

The script below downloads the rss in the $url variable and stores it locally as test.xml in the same directory where the script is.

 

Great until here, all working fine.

Now I change the url in $url, I need to download:

$url = "https://mux54.axion.es:15008/tmira/tmDB/epg/data/1/xmls/20100517.xml";

 

 

(You won't be able to see

https://mux54.axion.es:15008/tmira/tmDB/epg/data/1/xmls/20100517.xml

because is an IP restricted url that I'm allowed to see, but I'm attaching the xml source right after the script with fake info)

 

My server's IPs have permision to grab the file, I have no blocks.

But what happens is that I've got two errors:

 

Notice: Undefined variable: buffer in /var/www/vhosts/server.net/httpdocs/folder/1.php  on line 25

pointing to: 

$buffer .= $tmp;

 

Notice: Undefined offset: 1 in /var/www/vhosts/server.net/httpdocs/folder/1.php on line 29

pointing to:

$file_binary = substr($buffer, - $parts[1]);

 

and the local xml (test.xml) is created.

But instead of containing the remote xml contents it has:

 

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">

<html><head>

<title>400 Bad Request</title>

</head><body>

<h1>Bad Request</h1>

<p>Your browser sent a request that this server could not understand.<br />

Reason: You're speaking plain HTTP to an SSL-enabled server port.<br />

Instead use the HTTPS scheme to access this URL, please.<br />

</body></html>

 

What's happening? Anyone has a clue why I cannot download this remote file? 

Thanks a ton!

 

<?php

$url ="http://twitter.com/statuses/user_timeline/70640679.rss"; //working example
//$url = "https://mux54.axion.es:15008/tmira/tmDB/epg/data/1/xmls/20100517.xml"; //real file I have to download

$file_name = 'test.xml';

    if($file_name == NULL){ $file_name = basename($url);}
    $url_stuff = parse_url($url);
    $port = isset($url_stuff['port']) ? $url_stuff['port'] : 80;

    $fp = fsockopen($url_stuff['host'], $port);
    if(!$fp){ return false;}

    $query  = 'GET ' . $url_stuff['path'] . " HTTP/1.0\n";
    $query .= 'Host: ' . $url_stuff['host'];
    $query .= "\n\n";


    fwrite($fp, $query);

    while ($tmp = fread($fp, 8192))   {
        $buffer .= $tmp;
    }

    preg_match('/Content-Length: ([0-9]+)/', $buffer, $parts);
    $file_binary = substr($buffer, - $parts[1]);
    if($file_name == NULL){
        $temp = explode(".",$url);
        $file_name = $temp[count($temp)-1];
    }
    $file_open = fopen($file_name,'w');
    if(!$file_open){ return false;}
    fwrite($file_open,$file_binary);
    fclose($file_open);
    return true;
?>


///////////the xml file to download//////////////////////
//https://mux54.axion.es:15008/tmira/tmDB/epg/data/1/xmls/20100517.xml
////////////////////////////////////////////////

<BroadcastData>
<ProviderInfo>
<ProviderId>1</ProviderId>
<ProviderName>name</ProviderName>
</ProviderInfo>
<ScheduleData>
<ChannelPeriod endTime="2010-05-13T06:00:00+02:00" beginTime="2010-05-12T06:00:00+02:00">
<ChannelId>1</ChannelId>
<Event EventId="8911" duration="9000" beginTime="2010-05-12T06:00:00+02:00">
<EpgProduction>
<DvbContent>
<Content nibble2="0" nibble1="2"/>
</DvbContent>
<ParentalRating countryCode="ENG">0</ParentalRating>
<EpgText language="eng">
<ShortDescription>some words</ShortDescription>
<Description/>
<Name>a name</Name>
</EpgText>
</EpgProduction>
<EventType>S</EventType>
<FreeAccess>1</FreeAccess>
<Unscrambled>1</Unscrambled>
</Event>
<Event EventId="8912" duration="3600" beginTime="2010-05-12T08:30:00+02:00">
<EpgProduction>
<DvbContent>
<Content nibble2="0" nibble1="2"/>
</DvbContent>
<ParentalRating countryCode="ENG">0</ParentalRating>
<EpgText language="eng">
<ShortDescription>Redifusión</ShortDescription>
<Description/>
<Name>a title</Name>
</EpgText>
</EpgProduction>
<EventType>S</EventType>
<FreeAccess>1</FreeAccess>
<Unscrambled>1</Unscrambled>
</Event>
<Event EventId="8913" duration="10800" beginTime="2010-05-12T09:30:00+02:00">
<EpgProduction>
<DvbContent>
<Content nibble2="3" nibble1="11"/>
</DvbContent>
<ParentalRating countryCode="ESP">0</ParentalRating>
<EpgText language="spa">
<ShortDescription>Redifusión</ShortDescription>
<Description/>
<Name>some name</Name>
</EpgText>
</EpgProduction>
<EventType>S</EventType>
<FreeAccess>1</FreeAccess>
<Unscrambled>1</Unscrambled>
</Event>
.
.

</ChannelPeriod>
</ScheduleData>
</BroadcastData>

Link to comment
https://forums.phpfreaks.com/topic/202065-why-is-not-downloading-the-remote-file/
Share on other sites

I think what may be your issue (not sure, really just skimming through this lack of time sorry...), but it seems like it just doesn't like the fact that your $buffer variable is undefined the first round through that while loop. So when you try to append a value to the undefined variable the script dies.

 

Try changing this just for S & G's and see if it gets rid of that first error.

    fwrite($fp, $query);

    while ($tmp = fread($fp, 8192))   {
        $buffer .= $tmp;
    }


////////////////////change to below///////////////////

    fwrite($fp, $query);

    while ($tmp = fread($fp, 8192))   {
        
        if (isset($buffer){ $buffer .= $tmp; }
        if (!isset($buffer)){ $buffer = $tmp; }
    }

What is said above is true, but it is not your main problem. As the file that is being returned states, you are sending a standard HTTP request to your HTTPS server. I'm not sure exactly what to change, but I believe that it is this line:

 

$query  = 'GET ' . $url_stuff['path'] . " HTTP/1.0\n";

This may be of further assistance:

 

When you try to POST/GET requests via HTTPS over SSL/TLS you should notice this:

 

<?php

// preconditions

$port = 80 | 443

$host = "www.example.com";

$method = "POST" | "GET";

$contenttype = "text/html" | "text/plain" | "text/xml" | ...;

$data = "<something>";

 

// script

if($port == 443)

      $sslhost = "ssl://".$host;

else

      $sslhost = $host;

$fp = fsockopen($sslhost, $port);

fputs($fp, "$method $path HTTP/1.1\r\n");

fputs($fp, "Host: $host\r\n");

fputs($fp, "Content-type: $contenttype\r\n");

fputs($fp, "Content-length: ".strlen($data)."\r\n");

fputs($fp, "Connection: close\r\n");

fputs($fp, "\r\n");

?>

 

The server usually does not understand the HTTP-header "Host: XXX" if you provide it with the trailing "ssl://" used by fsockopen(); If you do it anyway you probably get a HTTP 400 back as response. :-)

Just in case is needed, it's as simple as this:

 

<?php
$pathh="https://mux54.axion.es:15008/tmira/tmDB/epg/data/1/xmls/";
$datee= date('Ymd');
$content=file_get_contents($pathh.$datee .".xml");
$file = "test.xml";
$Saved_File = fopen($file, 'w');
fwrite($Saved_File, $content);
fclose($Saved_File);
?>

 

Thanks a lot !!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.