Download a file via CURL with Cookies using php [closed]

99封情书 提交于 2019-12-01 13:24:52

问题


I need to POST cookies.txt then download a page to a text file using CURL. This is my FGC example but apparently FGC is bad at cookies so I need CURL.

<?php
    $file = file('source\1.txt');
    foreach ($file as $link)
    {
        $link       = trim($link);
        $link2      = "http://site-that.com/authenticates?={$link}";
        $downloaded = file_get_contents($link2);
        $myFile     = "parsed/$link" . ".txt";
        $fh = fopen($myFile, 'a') or die('Cannot open file');
        fwrite($fh, $downloaded);
    }
    $timestamp = time();
    rename('source\1.txt', "source/done/done-{$timestamp}.txt");
    echo 'Finished';

Any ideas? Code examples would be extremely appreciated. A simple example that does this to like google.com would be great Also, if you have another way to do this that's faster, please post!


回答1:


$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_COOKIEFILE, $cookieFileName); //$cookieFilename must be a path to a file with cookie data
curl_setopt($curl, CURLOPT_COOKIEJAR, $cookieFileName);
//curl_setopt($curl, CURLOPT_COOKIE, $cookie);  //you may also use this, here $cookie is a cookie string
curl_close($curl);
$data = curl_exec($curl);


来源:https://stackoverflow.com/questions/16656611/download-a-file-via-curl-with-cookies-using-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!