1024programmer PHP Another way for PHP to use curl to transfer files

Another way for PHP to use curl to transfer files

2019 unicorn companies pay a lot of money to recruit Python engineers>>> hot3.png

<?php$boundary = '------------------- ---------168279961491';
// our request body
$str = "$boundary\r\nContent-Disposition: form-data; name=& #39;how_do_i_turn_you'\r\n\r\non\r\n$boundary--\r\n";// set up cURL
$ch=curl_init('http: //example.com/');
curl_setopt_array($ch, array(CURLOPT_HEADER => false, CURLOPT_RETURNTRANSFER => true, CURLOPT_POST => true, CURLOPT_HTTPHEADER => true, CURLOPT_HTTPHEADER = ;> array( // we need to send these two headers'Content-Type: multipart/form-data; boundary='.$boundary,'Content-Length: ' .strlen($str)), // note, do not set the CURLOPT_POSTFIELDS setting CURLOPT_READFUNCTION => 'myfunc'
));// function to stream data
// I= 39;m not sure what the file pointer $fp does in this context
// but $ch is the cURL resource handle, and $len is how many bytes to read
function myfunc($ch, $fp, $len) {static $pos=0; // keep track of positionglobal $str;// set data$data = substr($str, $pos, $len);// increment $pos$pos += strlen($data);// return the data to send in the request return $data;
}// execute request, and show output for lolz
echo curl_exec($ch) ;
curl_close($ch);

Streaming POST data through PHP cURL Using CURLOPT_READFUNCTION

Transfer: https://my.oschina.net/swingcoder/blog/896846

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/51408

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索