php采集图片,当参数名为"@绝对路径",这时 CURL 會幫你做 multipart/form-data 編碼
$img = file_get_contents('https://image.talicai.com/YmQzY2MyNWE4NGMzYjlhODA4ZTZiYTIyOTg1ZjRiMWM'); file_put_contents('C:/test/1.jpg', $img);
(PHP 5 <= 5.5.0)
$params = array( 'file' => '@/Pictures/753161072647248225.jpg', 'file_1' => '@/Pictures/753161072647248225.jpg', 'file_2' => '@/Pictures/753161072647248225.jpg', ); $ch = curl_init(); curl_setopt( $ch, CURLOPT_URL, $url ); curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 ); curl_setopt( $ch, CURLOPT_POST, true ); curl_setopt( $ch, CURLOPT_POSTFIELDS, $params ); $response = curl_exec($ch);
使用 CURLFile 类来处理文件,如下:(PHP 5 >= 5.5.0, PHP 7)
$url = 'http://127.0.0.1/test3.php'; $file = __DIR__ .'/0634134726bc5b8b.jpg'; $data = array('mypic'=>new CURLFile($file)); $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_POSTFIELDS, $data); $content = curl_exec($curl); curl_close($curl); print_r($content);
还可以这样写
< form action =“< ;?php echo $ _SERVER ['PHP_SELF'];?>“ method =“post”enctype =“multipart / form-data”> < label for =“file”>文件名:< / label> < input type =“file”name =“Filedata”id =“Filedata”/> < br /> < input type =“submit”name =“submit”value =“Submit”/> < / form> <?php if($ _POST ['submit']){ $ uploadDir =“/ uploads /”; $ RealTitleID = $ _FILES ['Filedata'] ['name']; $ ch = curl_init(“http://www.remotesite.com/upload.php”); curl_setopt($ ch,CURLOPT_RETURNTRANSFER,true); curl_setopt($ ch,CURLOPT_POST,1); curl_setopt($ ch,CURLOPT_RETURNTRANSFER,true); $ args ['file'] = new CurlFile($ _ FILES ['Filedata'] ['tmp_name'],'file / exgpd',$ RealTitleID); curl_setopt($ ch,CURLOPT_POSTFIELDS,$ args); $ result = curl_exec($ ch); } ?>