SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Hybrid View

  1. #1
    SitePoint Zealot w1nk5's Avatar
    Join Date
    Aug 2009
    Location
    Ontario, Canada
    Posts
    115
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    cURL CURLOPT_WRITEFUNCTION and tmpfile()

    This code works:

    PHP Code:
    // Create temp file to write to
    $fp_tmp tmpfile();

    $ch curl_init();
    curl_setopt($chCURLOPT_URL$name);
    curl_setopt($chCURLOPT_FILE$fp_tmp);
    curl_setopt($chCURLOPT_VERBOSE1);
    curl_setopt($chCURLOPT_AUTOREFERERfalse);
    curl_setopt($chCURLOPT_REFERER'https://dl.dropboxusercontent.com');
    curl_setopt($chCURLOPT_HTTP_VERSIONCURL_HTTP_VERSION_1_1);
    curl_setopt($chCURLOPT_HEADER0);

    $result curl_exec($ch);
    curl_close($ch);

    // Write the files
    $fp fopen($orderfile->getFileLocation(), 'w');
    stream_copy_to_stream($fp_tmp$fp); 
    This code does not:

    PHP Code:
    // Create temp file to write to
    $fp_tmp tmpfile();

    $ch curl_init();
    curl_setopt($chCURLOPT_URL$name);
    curl_setopt($chCURLOPT_VERBOSE1);
    curl_setopt($chCURLOPT_AUTOREFERERfalse);
    curl_setopt($chCURLOPT_REFERER'https://dl.dropboxusercontent.com');
    curl_setopt($chCURLOPT_HTTP_VERSIONCURL_HTTP_VERSION_1_1);
    curl_setopt($chCURLOPT_HEADER0);
    curl_setopt($chCURLOPT_WRITEFUNCTION, function($ch$str) use (&$fp_tmp) {
        
    $length fwrite($fp_tmp$str);
        return 
    $length;
    });

    $result curl_exec($ch);
    curl_close($ch);

    // Write the files
    $fp fopen($orderfile->getFileLocation(), 'w');
    stream_copy_to_stream($fp_tmp$fp); 
    I am assuming that I cannot pass a stream via the 'use' to a function this way as it fails to copy the data. I guess my question is, how can I write the content of $str to $fp_tmp using CURLOPT_WRITEFUNCTION?

  2. #2
    SitePoint Zealot w1nk5's Avatar
    Join Date
    Aug 2009
    Location
    Ontario, Canada
    Posts
    115
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm a dummy. I forgot to reset the pointer using fseek().

    PHP Code:
    // Create temp file to write to 
    $fp_tmp tmpfile(); 

    $ch curl_init(); 
    curl_setopt($chCURLOPT_URL$name); 
    curl_setopt($chCURLOPT_VERBOSE1); 
    curl_setopt($chCURLOPT_AUTOREFERERfalse); 
    curl_setopt($chCURLOPT_REFERER'https://dl.dropboxusercontent.com'); 
    curl_setopt($chCURLOPT_HTTP_VERSIONCURL_HTTP_VERSION_1_1); 
    curl_setopt($chCURLOPT_HEADER0); 
    curl_setopt($chCURLOPT_WRITEFUNCTION, function($ch$str) use (&$fp_tmp) { 
        
    $length fwrite($fp_tmp$str); 
        return 
    $length
    }); 

    $result curl_exec($ch); 
    curl_close($ch); 

    // Write the files 
    $fp fopen($orderfile->getFileLocation(), 'w'); 
    fseek($fp_tmp0);
    stream_copy_to_stream($fp_tmp$fp); 


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •