上传图片到cdn服务器

 1 /**
 2  * 发送POST请求
 3  */
 4 function doPost2($url, $postdata) {
 5     //初始化
 6     $curl = curl_init();
 7        // 设置post方式提交
 8     curl_setopt($curl, CURLOPT_POST, 1);
 9     // 设置抓取的url
10     curl_setopt($curl, CURLOPT_URL, $url);
11     // 设置提交的数据
12     curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata);
13     // 设置获取的信息以文件流的形式返回,而不是直接输出。
14     curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
15     //设置头文件的信息作为数据流输出
16     curl_setopt($curl, CURLOPT_HTTPHEADER, array('application/x-www-form-urlencoded;charset=utf-8'));
17 
18     $res = curl_exec($curl);
19     //关闭URL请求
20     curl_close($curl);
21 
22     return $res;
23 }
 1 /**
 2 *   上传图片到cdn服务器上
 3 *   @param string file_path  图片的绝对地址
 4 *   @return string  返回图片地址
 5 */
 6 function upload_cdn($file_path) {
 7     if (class_exists('CURLFile')) {// 这里用特性检测判断php版本
 8         $file_path = new CURLFile(realpath($file_path));//>=5.5
 9     } else {
10         $file_path = '@'.$file_path;//<=5.5
11     }
12 
13     $url = "服务器地址";
14     
15     $data = array(
16         'imgfile1'=>$file_path,
17         'PHPSESSID'=>'ba4d1587aab023fd8cfa28fbe36c8235',
18         'totalform'=>'1'
19     );
20 
21     $result = doPost2($url,$data);
22 
23     return $result;
24 
25 }
原文地址:https://www.cnblogs.com/lonmyblog/p/7047609.html