1

I have php a code and can't understand why the php script creates only 4 files, but without the data. If I use curl only for one url without foreach loop all will be ok. Any thoughts?

$years = array('2012', '2013', '2014', '2015');
foreach($years as $year)
{
    $url = "http://lpo.dt.navy.mil/data/DM/Environmental_Data_Deep_Moor_{$year}.txt";
    $fp = fopen(base_path() . "/database/rawdata/{$year}.txt", 'w');

    $curl = curl_init();
    curl_setopt_array($curl, array(
        CURLOPT_URL             => $url,
        CURLOPT_FILE            => $fp,
        CURLOPT_VERBOSE         => 1,
        CURLOPT_RETURNTRANSFER  => 1,
        CURLOPT_FAILONERROR     => 1
    ));

    curl_exec($curl);

    if (!curl_errno($curl))
    {
        $info = curl_getinfo($curl);
        Log::info("File created  {$year}.txt");
        Log::notice("Evaluation of this script was {$info['total_time']} seconds!");
    }
    else
    {
        Log::error("cURL ERROR " . curl_error($curl));
    }

    curl_close($curl);
    fclose($fp);
}
3
  • You should make an edit and include your code. Commented Sep 12, 2015 at 13:47
  • Where's the code that uses $fp ? you don't seem to have any code that handle the file write so why wouldn't it be empty? Commented Sep 12, 2015 at 14:05
  • CURLOPT_FILE => $fp, Commented Sep 12, 2015 at 14:08

1 Answer 1

3

See joeterranova's comment on the PHP site:

It appears that setting CURLOPT_FILE before setting CURLOPT_RETURNTRANSFER doesn't work, presumably because CURLOPT_FILE depends on CURLOPT_RETURNTRANSFER being set

.

Therefore you should change your curl_setopt_array to the following:

curl_setopt_array($curl, array(
    CURLOPT_URL             => $url,
    CURLOPT_RETURNTRANSFER  => 1,
    CURLOPT_FILE            => $fp,
    CURLOPT_VERBOSE         => 1,
    CURLOPT_FAILONERROR     => 1
));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.