Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用curl和php将字符串作为文件发送_Php_Post_Curl - Fatal编程技术网

使用curl和php将字符串作为文件发送

使用curl和php将字符串作为文件发送,php,post,curl,Php,Post,Curl,我知道我可以使用这个语法使用php、post和curl发送文件 $post = array( "file_box"=>"@/path/to/myfile.jpg", ); curl_setopt($ch, CURLOPT_POSTFIELDS, $post); 如何获取字符串、构建临时文件并使用完全相同的语法发送它 更新: 我更喜欢使用tmpfile()或php://memory 因此,我不必处理文件创建。您可以使用文件内容创建临时文件,只需确保目标目录可写即可 $path =

我知道我可以使用这个语法使用php、post和curl发送文件

$post = array(
    "file_box"=>"@/path/to/myfile.jpg",
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post); 
如何获取字符串、构建临时文件并使用完全相同的语法发送它

更新:
我更喜欢使用tmpfile()或php://memory 因此,我不必处理文件创建。

您可以使用
文件内容
创建临时文件,只需确保目标目录可写即可

$path = '/path/to/myfile.txt';    
file_put_contents($myData, $path);

$post = array(
    "file_box"=>"@".$path,
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);

# Delete the file if you don't need it anymore
unlink($path);

您可以在临时目录中使用创建文件:

$string = 'random string';

//Save string into temp file
$file = tempnam(sys_get_temp_dir(), 'POST');
file_put_contents($file, $string);

//Post file
$post = array(
    "file_box"=>'@'.$file,
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);

//do your cURL work here...

//Remove the file
unlink($file);


这个问题的副本:(你会在那里找到你的答案)不是真正的答案@到目前为止,emil给出了一个很好的解决方案。呃,是的,当文件的内容是字符串时,通过curl发送文件是一个真正的答案。Tatu的答案是另一种涉及临时文件的解决方案(因此您发送的不是字符串作为文件,而是实际文件)。
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);