0

There is a code sample that downloads a content of a web page.

The problem: a zero lenght file appears on HDD always.

I believe this code works in many cases, but I am interesting to know how to fix it in the case of mine.

<?php

$url = 'https://wtfismyip.com/text';
$destFile = 'experiment.tmp';

$fp = fopen($destFile, 'w+b');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_FILE, $fp); // write curl response to file
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_exec($ch);
curl_close($ch);
fclose($fp);

I tried different URLS with no luck. Another approach, via fopen + fread, works fine.

  • Windows 7
  • PHP 7.1.0 (cli) (built: Dec 2 2016 05:24:57) ( ZTS MSVC14 (Visual C++ 2015) x64 )
  • Executed just as php example.php
Pavel Vlasov
  • 4,206
  • 6
  • 41
  • 54
  • 1
    Could you try getting the HTTP status code after the transfer? `curl_getinfo($ch, CURLINFO_HTTP_CODE);` – BradzTech Jan 06 '17 at 23:24
  • 2
    `CURLOPT_BINARYTRANSFER` is redundant, although removing that line probably won't make any difference. Why aren't you checking the value returned by `curl_exec()`? – r3mainer Jan 06 '17 at 23:28
  • Got the same problem. The file is there but 0KB. The curl_exec ends successfully after 2-3 seconds - without errors. After reading posts in forums the file had 45MB. After the coffee break it already had 85MB. Therefore I think that curl works time-delayed??? (My current solution: only edit the file after one hour). – Sarah Trees Mar 18 '20 at 09:08
  • https://stackoverflow.com/questions/3757071/php-debugging-curl – Don't Panic Jul 16 '23 at 09:19

2 Answers2

0

there is nothing wrong with your code, it works fine here, but there's no error checking anywhere, and you probably get some error somewhere. this should catch your error, try it

<?php
declare(strict_types=1);
error_reporting(E_ALL);
ini_set('display_errors','1');
$url = 'https://wtfismyip.com/text';
$destFile = 'experiment.tmp';

$fp =fopen($destFile, 'w+b');
if (false === $fp) {
    throw new RuntimeException ( 'fopen() failed.   last error: ' . return_var_dump ( error_get_last () ) );
}
$ch = curl_init();
if (false === $ch) {
    throw new RuntimeException ( 'curl_init() failed.   last error: ' . return_var_dump ( error_get_last () ) );
}

ecurl_setopt($ch, CURLOPT_URL,$url);
ecurl_setopt($ch, CURLOPT_FILE, $fp); // write curl response to file
ecurl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
ecurl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
ecurl_setopt($ch, CURLOPT_VERBOSE, true);
ecurl_setopt($ch, CURLOPT_STDERR, $fp);
ecurl_exec($ch);
curl_close($ch);
fclose($fp);



function ecurl_setopt ( /*resource*/$ch , int $option , /*mixed*/ $value ):bool{
    $ret=curl_setopt($ch,$option,$value);
    if($ret!==true){
        //option should be obvious by stack trace
        throw new RuntimeException ( 'curl_setopt() failed. curl_errno: ' . return_var_dump ( curl_errno ($ch) ).'. curl_error: '.curl_error($ch) );
    }
    return true;
}
function ecurl_exec ( /*resource*/$ch)/*:mixed depending on CURLOPT_RETURNTRANSFER*/{
    $ret=curl_exec($ch);
    if($ret===false){
        throw new RuntimeException ( 'curl_exec() failed. curl_errno: ' . return_var_dump ( curl_errno ($ch) ).'. curl_error: '.curl_error($ch) );
    }
    return $ret;
}


function return_var_dump(/*...*/){
    $args = func_get_args ();
    ob_start ();
    call_user_func_array ( 'var_dump', $args );
    return ob_get_clean ();
}
hanshenrik
  • 19,904
  • 4
  • 43
  • 89
0

Quite often, this happens when trying to copy a file from an HTTPS server. Try skipping the certificate verification:

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
nourish
  • 1,425
  • 1
  • 11
  • 9