zoukankan      html  css  js  c++  java
  • 【微信上传素材接口--临时&永久性】微信临时&永久性上传、获取返回的medie_id 和url

    上传图片到微信服务器获得media_id和url (临时&永久性)

    写在前面:php>=5.6,使用curlFile类,php<5.6使用@realpath(file)

    其他接口类:https://www.cnblogs.com/gjw-hsf/p/7375261.html

    转载地址:https://blog.csdn.net/httIsAWang/article/details/71576880

    临时3天内有效;post请求url:

    临时3天接口:
    $url = "https://api.weixin.qq.com/cgi-bin/media/upload?access_token=".$token."&type=".$type;
    永久素材接口:
    $url = "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=".$token."&type=".$type;

    如果是本地选择图片并上传可能需要上传到服务器并在服务端进行post;

    0、先是获取token;

    function get_token($id, $s){
       $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='$id'&secret='$s'";
                
                   $ch = curl_init();
                   curl_setopt($ch, CURLOPT_URL, $url);
                   curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
                   curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); 
                   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                   $output = curl_exec($ch);
                   curl_close($ch);
                   $jsoninfo = json_decode($output, true);    
                   $token = $jsoninfo["access_token"];
                return $token;
    }

    1、获取media_id和url   --- $filepath 必须得是 realpath(public_path('images/a1.png'))绝对路径

    PHP的cURL支持通过给CURL_POSTFIELDS传递关联数组(而不是字符串)来生成multipart/form-data的POST请求。
    
    传统上,PHP的cURL支持通过在数组数据中,使用“@+文件全路径”的语法附加文件,供cURL读取上传。这与命令行直接调用cURL程序的语法是一致的:
    
    
    $type = "image";  //声明上传的素材类型,这里为image
    $token = get_access_token();//调用接口需要获取token,这里使用一个封装好的调取access_token的函数
    $url = "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=".$token."&type=".$type;
            //这里是请求地址,token和素材类型通过get方式传递
    $file_path = dirname(__FILE__)."/pro.jpg";
            //这里声明文件的路径,使用绝对路径
    $file_data = array('media'  => '@'.$file_path);
            //传递的数组,方式一:使用'@'符号加上文件的绝对路径来指引文件。这种方式适合PHP5.5之前的版本,
    $file_data = array("media"  => new CURLFile($file_path));
            //传递的数组,方式二:从PHP5.5版本以后,引入了新的CURLFile 类来指向文件,参数传入的也是绝对路径
    $ch = curl_init();
    curl_setopt($ch , CURLOPT_URL , $url);
    curl_setopt($ch , CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch , CURLOPT_POST, 1);
    //发送一个POST请求
    curl_setopt($ch , CURLOPT_POSTFIELDS, $file_data);
    //传递一个关联数组,生成multipart/form-data的POST请求
    $output = curl_exec($ch);//发送请求获取结果
    curl_close($ch);//关闭会话
    return $output;//返回结果
    返回结果:
    
    {"media_id":"Y-wN-hbQ42QYeA5-956YrLgmGDTnl4bWqJr6AjsgoGk",
    
    "url":"http://mmbiz.qpic.cn/mmbiz_jpg/gyNXEoR9bg5UfM2wEFcz3tYO4a0adXo8Y4treMlnStHt8E0mDZazhxmGiamfU06tDkzCt55YR681Npd9sasH0xg/0?wx_fmt=jpeg"
    
    }

    文档:https://mp.weixin.qq.com/wiki/10/10ea5a44870f53d79449290dfd43d006.html

    测试工具接口:https://mp.weixin.qq.com/debug/cgi-bin/apiinfo?t=index&type=%E5%9F%BA%E7%A1%80%E6%94%AF%E6%8C%81&form=%E5%A4%9A%E5%AA%92%E4%BD%93%E6%96%87%E4%BB%B6%E4%B8%8A%E4%BC%A0%E6%8E%A5%E5%8F%A3%20/media/upload

  • 相关阅读:
    16. 3Sum Closest
    17. Letter Combinations of a Phone Number
    20. Valid Parentheses
    77. Combinations
    80. Remove Duplicates from Sorted Array II
    82. Remove Duplicates from Sorted List II
    88. Merge Sorted Array
    257. Binary Tree Paths
    225. Implement Stack using Queues
    113. Path Sum II
  • 原文地址:https://www.cnblogs.com/xuzhengzong/p/9690644.html
Copyright © 2011-2022 走看看