Send string as a file using curl and php

后端 未结 3 861
忘了有多久
忘了有多久 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

    You can create a file using tempnam in your temp directory:

    $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);
    

提交回复
热议问题