Send string as a file using curl and php

后端 未结 3 879
忘了有多久
忘了有多久 2020-12-11 10:06

I know I can use this syntaxt to send a file using php, post and curl.

$post = array(
    \"file_box\"=>\"@/path/to/myfile.jpg\",
);
curl_setopt($ch, CURL         


        
3条回答
  •  难免孤独
    2020-12-11 10:59

    From http://php.net/manual/en/class.curlfile.php#115161

    function curl_custom_postfields($ch, array $assoc = array(), array $files = array()) {
        
                 // invalid characters for "name" and "filename"
                 static $disallow = array("\0", "\"", "\r", "\n");
    
                 // build normal parameters
                 foreach ($assoc as $k => $v) {
                     $k = str_replace($disallow, "_", $k);
                     $body[] = implode("\r\n", array(
                         "Content-Disposition: form-data; name=\"{$k}\"",
                         "",
                         filter_var($v),
                     ));
                 }
    
                 // build file parameters
                 foreach ($files as $k => $v) {
                     switch (true) {
                         case false === $v = realpath(filter_var($v)):
                         case !is_file($v):
                         case !is_readable($v):
                             continue; // or return false, throw new InvalidArgumentException
                     }
                     $data = file_get_contents($v);
                     $v = call_user_func("end", explode(DIRECTORY_SEPARATOR, $v));
                     $k = str_replace($disallow, "_", $k);
                     $v = str_replace($disallow, "_", $v);
                     $body[] = implode("\r\n", array(
                         "Content-Disposition: form-data; name=\"{$k}\"; filename=\"{$v}\"",
                         "Content-Type: image/jpeg",
                         "",
                         $data,
                     ));
                 }
    
                 // generate safe boundary
                 do {
                     $boundary = "---------------------" . md5(mt_rand() . microtime());
                 } while (preg_grep("/{$boundary}/", $body));
    
                 // add boundary for each parameters
                 array_walk($body, function (&$part) use ($boundary) {
                     $part = "--{$boundary}\r\n{$part}";
                 });
    
                 // add final boundary
                 $body[] = "--{$boundary}--";
                 $body[] = "";
    
                 // set options
                 return @curl_setopt_array($ch, array(
                     CURLOPT_POST       => true,
                     CURLOPT_POSTFIELDS => implode("\r\n", $body),
                     CURLOPT_HTTPHEADER => array(
                         "Expect: 100-continue",
                         "Content-Type: multipart/form-data; boundary={$boundary}", // change Content-Type
                     ),
                 ));
             }
    
    $array1=array('other_post_field'=>'value');
    $array2=array('file'=>'document_content_string');
    $ch = curl_init();       
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_custom_postfields($ch,$array1,$array2);//above custom function
    $output=curl_exec($ch);
    close($ch);
    

提交回复
热议问题