采集的抓取图片和图片上传

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); 
} 
?> 

  

转载于:https://www.cnblogs.com/matengfei123/p/8523492.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值