Link to home
Start Free TrialLog in
Avatar of sahanz
sahanz

asked on

[function.copy]: failed to open stream: HTTP request failed! HTTP/1.1 404

In my script I get this error, but the problem is the file I'm trying to copy is exist on the remote server, Why is that?
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Please post the script that gives this error, thanks.
Avatar of sahanz
sahanz

ASKER

Here it is,
Btw I tried to copy "http://www.google.com/logos/classicplus.png" and it worked.

[code]
<?php

$imageurl="http://fotoflexer.com/Normal/temp2/b70f937e915643d759a7cf6225e65edd.jpg";
copy($imageurl,"/home/public_html/picture/picture_album/usedef/20/sahan.jpg");
[/code]
Try using something more like this...

$image = file_get_contents("http://fotoflexer.com/Normal/temp2/b70f937e915643d759a7cf6225e65edd.jpg");
file_put_contents("picture/picture_album/usedef/20/sahan.jpg", $image);

Use error_reporting(E_ALL) at the top of the script and test the functions for appropriate returns, too!
Avatar of sahanz

ASKER

same error :(
Interesting... We may need to use CURL to get the remote file.  A simple "read" operation did not seem to do the job.  Try running this and see what you get!
<?php // RAY_temp_sahanz.php
error_reporting(E_ALL);
echo "<pre>";

// REMOTE FILE
$url = 'http://fotoflexer.com/Normal/temp2/b70f937e915643d759a7cf6225e65edd.jpg';

// LOCAL FILE
$fnm = 'RAY_temp_sahanz.jpg';

// READ AND WRITE
$img = file_get_contents($url);
if (!$img) die("CANNOT READ $url");
$res = file_put_contents($fnm, $img);
if (!$res) die("CANNOT WRITE $url to $fnm");

// SHOW LOCAL LINK
echo "<a href=\"$fnm\">LOCAL COPY OF $url</a>" . PHP_EOL;

Open in new window

Avatar of sahanz

ASKER

ok Here's the feedback,

-------------------------------------------------------------------------------------------------------------------------


Warning:  file_get_contents(http://fotoflexer.com/Normal/temp2/b70f937e915643d759a7cf6225e65edd.jpg) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found
 in /home/movievi1/public_html/picture/ffsave.php on line 19

CANNOT READ http://fotoflexer.com/Normal/temp2/b70f937e915643d759a7cf6225e65edd.jpg
----------------------------------------------------------------------
Avatar of sahanz

ASKER

Here's what I get when I run it on my wamp.

Notice: Undefined variable: content_type in G:\wamp\www\ffsave.php on line 23

Warning: copy(/path/to/images/image_name.) [function.copy]: failed to open stream: No such file or directory in G:\wamp\www\ffsave.php on line 30

I think its working fine under my local pc :) There might be some kind of server prob ?
Even more interesting... This did not work either!  They are doing something tricky on their end to prevent you from getting the files in an automated way.  Do you have permission from fotoflexer to do this?  If so, they should provide you with an API.
function my_curl($url, $timeout=2, $error_report=FALSE)
{
    $curl = curl_init();

    // HEADERS FROM FIREFOX - APPEARS TO BE A BROWSER REFERRED BY GOOGLE
    $header[] = "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
    $header[] = "Cache-Control: max-age=0";
    $header[] = "Connection: keep-alive";
    $header[] = "Keep-Alive: 300";
    $header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
    $header[] = "Accept-Language: en-us,en;q=0.5";
    $header[] = "Pragma: "; // browsers keep this blank.

    // SET THE CURL OPTIONS - SEE http://php.net/manual/en/function.curl-setopt.php
    curl_setopt($curl, CURLOPT_URL,            $url);
    curl_setopt($curl, CURLOPT_USERAGENT,      'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6');
    curl_setopt($curl, CURLOPT_HTTPHEADER,     $header);
    curl_setopt($curl, CURLOPT_REFERER,        'http://www.google.com');
    curl_setopt($curl, CURLOPT_ENCODING,       'gzip,deflate');
    curl_setopt($curl, CURLOPT_AUTOREFERER,    TRUE);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE);
    curl_setopt($curl, CURLOPT_TIMEOUT,        $timeout);

    // RUN THE CURL REQUEST AND GET THE RESULTS
    $htm = curl_exec($curl);
    $err = curl_errno($curl);
    $inf = curl_getinfo($curl);
    curl_close($curl);

    // ON FAILURE
    if (!$htm)
    {
        // PROCESS ERRORS HERE
        if ($error_report)
        {
            echo "CURL FAIL: $url TIMEOUT=$timeout, CURL_ERRNO=$err";
            var_dump($inf);
        }
        return FALSE;
    }

    // ON SUCCESS
    return $htm;
}

Open in new window

Avatar of sahanz

ASKER

fotoflexer has an API and its not like other API's out there. Check, http://fotoflexer.com/api.php/ (nothing read actually :) )

Yes they do provide permissions
I don't get a stream open failure - I get the name of the remote file.  Maybe I need to rebalance my medication.  Here is the response from CURL - the server is clearly producing a 404, but when I try the remote URL in Firefox, FF somehow negotiates its way to the JPG.  I will have to look at this some more!
array(20) {
  ["url"]=>
  string(71) "http://fotoflexer.com/Normal/temp2/b70f937e915643d759a7cf6225e65edd.jpg"
  ["content_type"]=>
  string(29) "text/html; charset=iso-8859-1"
  ["http_code"]=>
  int(404)
  ["header_size"]=>
  int(175)
  ["request_size"]=>
  int(511)
  ["filetime"]=>
  int(-1)
  ["ssl_verify_result"]=>
  int(0)
  ["redirect_count"]=>
  int(0)
  ["total_time"]=>
  float(0.115499)
  ["namelookup_time"]=>
  float(0.00351)
  ["connect_time"]=>
  float(0.059448)
  ["pretransfer_time"]=>
  float(0.059459)
  ["size_upload"]=>
  float(0)
  ["size_download"]=>
  float(311)
  ["speed_download"]=>
  float(2692)
  ["speed_upload"]=>
  float(0)
  ["download_content_length"]=>
  float(311)
  ["upload_content_length"]=>
  float(0)
  ["starttransfer_time"]=>
  float(0.115418)
  ["redirect_time"]=>
  float(0)
}

Open in new window

Avatar of sahanz

ASKER

1. It downloads the google logo
2. It dsnt work on webhosting servers I tried (2 linux)
3. It works perfect on my localhost.

Confuced.................
OK, it looks like fotoflexer expects to be called through JavaScript.  Apparently they do not expect a direct call to the URL.  So perhaps the fact that we can see the image when we use Firefox but cannot see the image when we use CURL is because FF follows the 404 error handler, and the error handler page sends the image?
http://fotoflexer.com/api.php
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Please see the code snippet for their example of the script.  Looks odd to me.  I think that $content_type is undefined in this code.  But maybe there is a germ of an idea in here.
<?php

// Parse paramters to get Image and thumbnail urls
$image = $_GET['image'];

// Verify that the URL begins with "http://fotos.fotoflexer.com"
// This step is used to ensure that the image is in authentic

if (strpos($image, "http://fotos.fotoflexer.com") != 0){
  //Handle the error:
  echo "Invalid Origin";
  exit;
}


// Grab the image type (png or jpeg)
$file_type = $content_type[1];

// Set the local file path for the image and thumbnail
$image_path = "/path/to/images/image_name." . $file_type;

// Image source and content have been verified, and a location has been set.
// Now copy the images to the local server.
copy($image,$image_path);

// Both image and thumb are on your server at the location specified!

// Optional:
// if this is is an intermediate page, you can now setup your server state
// and redirect

/*
header("Location:Path/AnotherPage.html");
*/

?>

Open in new window

Or maybe not... It looks like this thing is largely a FLASH + Javascript application, and apparently they do not expose the true URL of the image.  At least I could not find it.  Ask a moderator to add this to the JavaScript Zone and maybe an Expert with extensive knowledge of the DOM can show us why Firefix can render the image, but we cannot read it directly.

Best of luck with it, ~Ray
Avatar of sahanz

ASKER

Do u think something to do with php.ini ? fotoflexer has an example for the API, even it dsnt work on the server. And it works on my  localhost.
Avatar of sahanz

ASKER

The issue was from fotoflexer, Thanks for your help ray.
Gosh,sahanz -- wasn't my effort to help you worthy of ANY points at all?  I think there is a clear explanation at 33340806 and a suggestion to get additional help at 33341017.

What did I do wrong?
Avatar of sahanz

ASKER

OO Ray, I didnt mean that. Deleting a question is blackmark to u?
Well, I thought some of the research and testing I did would have been helpful to you.  But when I saw that this was more likely a JavaScript application, that is why I suggested you ask a moderator to add it to the JS zone.

If you found a solution, it is always the right thing to post it and accept your own answer.
Avatar of sahanz

ASKER

sorry for that :(
Avatar of sahanz

ASKER

The fotoflexer also had an issue
@sahanz, thank you (!) for the points.  Other than the occasional t-shirt it's all we get for our effort at EE, besides the gratification that comes from helping our colleagues.  I hope I can help you again in the future.   Best regards, ~Ray
Avatar of sahanz

ASKER

np ray, thanks again for the support
If you can modify the php.ini ypu can enable the fopen_wrapper that you need. In that case set
allow_url_fopen=1