700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > php实现以post方式上传图片文件办法实例

php实现以post方式上传图片文件办法实例

时间:2023-01-24 01:21:03

相关推荐

php实现以post方式上传图片文件办法实例

后端开发|php教程

post,php,图片

后端开发-php教程

本文主要和大家分享php实现以post方式上传图片文件的方法实例,在调用第三方api接口时,有时会遇到通过http协议上传图片,以下是一个微信公众平台新增永久素材的例子;

pc蛋蛋app源码,vscode打代码一直有光标,ubuntu共享无线,删除tomcat7卸载,抢淘宝爬虫,php导出怎么做,于洪区电商抖音seo优化方案lzw

php代码:

微信游戏 源码论坛,960m ubuntu,tomcat启动程序没进程,爬虫读取新闻,php中静态方法,正规的seo推广有哪些关键词lzw

/* 使用curl函数 */ $url = "https://api./cgi-bin/material/add_material?access_token=ACCESS_TOKEN&type=image"; $post_data = array( media => @bag03.jpg, ); $response = curl_http($url, POST, $post_data); $params = array(); $params = json_decode($response,true); if (isset($params[errcode])) { echo "error:" . $params[errcode]; echo "msg :" . $params[errmsg]; exit; } var_dump( $params ); /*** http请求方式: 默认GET*/ function curl_http($url, $method="GET", $postfields){ $ch = curl_init(); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($ch, CURLOPT_URL, $url); switch ($method) { case "POST":curl_setopt($ch, CURLOPT_POST, true);if (!empty($postfields)) { $hadFile = false; if (is_array($postfields) && isset($postfields[media])) { /* 支持文件上传 */ if (class_exists(\CURLFile)) { curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true); foreach ($postfields as $key => $value) {if (isPostHasFile($value)) { $postfields[$key] = new \CURLFile(realpath(ltrim($value, @))); $hadFile = true;} } } elseif (defined(CURLOPT_SAFE_UPLOAD)) { if (isPostHasFile($value)) {curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);$hadFile = true; } } } $tmpdatastr = (!$hadFile && is_array($postfields)) ? http_build_query($postfields) : $postfields; curl_setopt($ch, CURLOPT_POSTFIELDS, $tmpdatastr);}break; default:curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); /* //设置请求方式 */break; } $ssl = preg_match(/^https:\/\//i,$url) ? TRUE : FALSE; curl_setopt($ch, CURLOPT_URL, $url); if($ssl){ curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // https请求 不验证证书和hosts curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); // 不从证书中检查SSL加密算法是否存在 } $response = curl_exec($ch); curl_close($ch); if(empty($response)){ exit("错误请求"); } return $response; } function isPostHasFile($value) { if (is_string($value) && strpos($value, @) === 0 && is_file(realpath(ltrim($value, @)))) { return true; } return false; }

也可以使用php内置的系统函数,如果使用过程中出现问题,建议查看是否启用相应的系统函数。

鲜花商城移动端源码,ubuntu设置开机终端,爬虫find和search,php 论坛 商城,云南seo方案lzw

使用exec系统函数:

/* 使用exec函数 */$command = curl -F media=@.$filepath. "https://api./cgi-bin/media/upload?access_token=ACCESS_TOKEN&type=image";$retval = array();exec($command, $retval, $status);$params = array();$params = json_decode($retval[0],true);if ($status != 0) { $params = array( errcode => -100, errmsg => 公众号服务出错,请联系管理员, );}return $params;

使用system系统函数:

/* 使用system函数 */$command = curl -F media=@.$filepath. "https://api./cgi-bin/media/upload?access_token=ACCESS_TOKEN&type=image";$retval = 1;$last_line = system($command, $retval);$params = array();$params = json_decode($last_line,true);if ($retval != 0) { if (isset($params[errcode])) { $params = array( errcode => -100, errmsg => 公众号服务出错,请联系管理员, ); }}return $params;

实例讲解ajax实现用户名校验的传统和jquery的$.post方式、

ajax异步请求post方式

php模拟post方式调用接口请求代码实例

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。