1

我的 public_html 文件夹中有一个文件。我想通过 curl 将文件发送到其他 url。我正在读取文件的二进制代码并尝试发送到另一个 url。但是在接收端我无法获取文件,尽管我能够获取其他参数,例如 from , to 和 subject 但不是文件。下面是读取和发送文件的代码。

// ini attachments arrar
$arrAttachments = array();
echo 'attaching files ...<br>';
//dummy attachments files 
$arrFile = array('0'=>'running.jpg',
                 '1'=>'mail-doc.docx',
                 '2'=>'mail-sheet.xlsx',
                );
//get the binary of each attachments file
if(is_array($arrFile) && count($arrFile)>1) {

echo 'reading  files ...<br>';  
foreach ($arrFile as $key => $value) {
    //file location 
    $file = 'att/'.$value;
    $handle = fopen($file, "rb");

    //file size
    $fsize = filesize($file);

    //read file 
    $contents = fread($handle, $fsize);

    fclose($handle);

    //make binary data array 
    $arrAttachments[$key]['file_name']  =  $value;
    $arrAttachments[$key]['file_value'] = $contents;
}   
}

echo 'making data for post ...<br>';    
//whole email body with header
$emailContent  = array(
                        'attachments'=>json_encode($arrAttachments),    
                        'from'=>'mado023@gmail.com',
                        'to'=>'1234@customer3sixty.com',
                        'subject'=>'This is subject',
                        'body'=>'This is body',
                        );

echo 'curl request ini ...<br>';                    
//mail parser url 
$url ='http://localhost/c360/cron/t/';
//send the email content to url via curl
$ch = curl_init($url); //ini curl
curl_setopt_array($ch,array(
                            CURLOPT_RETURNTRANSFER => 1,
                            CURLOPT_TIMEOUT => 50,
                            CURLOPT_CONNECTTIMEOUT => 0,
                            CURLOPT_SSL_VERIFYHOST => 0,
                            CURLOPT_SSL_VERIFYPEER => 0
                            )
                            );
//set the post filed                            
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($emailContent));

//execute curl 
$response = curl_exec($ch);

在接收端我试图记录传入的数据。下面是接收参数和文件的代码。

$c .= 'from=>'.$_POST['from'] .' ';
        $c .= 'to=>'.$_POST['to'] .' ';
        $c .= 'subject=>'.$_POST['subject'] .' ';
        $c .= 'body=>'.$_POST['body'] .' ';
        //$c .= 'attachments=>'.json_encode($_POST['attachments']).' ';
        foreach ($_POST['attachments'] as $key => $value) {
            $c .= $value;
        }

        file_put_contents('pipe.log', $c);
4

0 回答 0