码迷,mamicode.com
首页 > Web开发 > 详细

php curl上传文件

时间:2017-06-06 15:53:35      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:pos   5.5   out   tran   timeout   color   本地   logs   使用   

如果PHP 5 >= 5.5.0, PHP 7,推荐使用CURLFile类设置文件信息

$real_path=$_SERVER[‘DOCUMENT_ROOT‘].‘/Public/Public/Images/1.jpg‘;//文件绝对路径
$data= array(
        "media"=>new CURLFile($real_path) 
        );
        $timeout = 5;
        $ch = curl_init ();
        curl_setopt ( $ch, CURLOPT_URL, $api );
        curl_setopt ( $ch, CURLOPT_POST, 1 );
        curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
        curl_setopt ( $ch, CURLOPT_CONNECTTIMEOUT, $timeout );
        curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, FALSE );
        curl_setopt ( $ch, CURLOPT_SSL_VERIFYHOST, false );
        curl_setopt ( $ch, CURLOPT_POSTFIELDS, $data);
        $result = curl_exec ( $ch );

根据php手册也可以这样设置,

$real_path=$_SERVER[‘DOCUMENT_ROOT‘].‘/Public/Public/Images/1.jpg‘;//文件绝对路径
        $data= array(
        "media"=>@$real_path /*注意@*/
        );
        $timeout = 5;
        $ch = curl_init ();
                curl_setopt ( $ch, CURLOPT_SAFE_UPLOAD,false );/*设置为false*/
        curl_setopt ( $ch, CURLOPT_URL, $api );
        curl_setopt ( $ch, CURLOPT_POST, 1 );
        curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
        curl_setopt ( $ch, CURLOPT_CONNECTTIMEOUT, $timeout );
        curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, FALSE );
        curl_setopt ( $ch, CURLOPT_SSL_VERIFYHOST, false );
        curl_setopt ( $ch, CURLOPT_POSTFIELDS, $data);
        $result = curl_exec ( $ch );

 然而我在本地测试成功.使用微信公众平台上传图片接口却失败,说找不到文件,有时间时要好好研究一下.

php curl上传文件

标签:pos   5.5   out   tran   timeout   color   本地   logs   使用   

原文地址:http://www.cnblogs.com/ch459742906/p/6951755.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!