zoukankan      html  css  js  c++  java
  • PHP 之上传网络图片到微信临时素材

    一、简单代码

    $fileImage = 'https://域名/202109/11/134923278422.png';
    $imgPathInfo =pathinfo($fileImage);
    $basename = $imgPathInfo['filename'].'png';
    $filename = dirname(__FILE__) . '/cache/image/' . $basename;
    //将网络图片保存到本地
    file_put_contents($filename, file_get_contents($fileImage));
    
    //判断是否支持curl_file_create函数
    if (!function_exists('curl_file_create')) {
        function curl_file_create($filename, $mimetype = '', $postname = '')
        {
            return "@$filename;filename="
                . ($postname ?: basename($filename))
                . ($mimetype ? ";type=$mimetype" : '');
        }
    }
    $access_token = getAccessToken($company_info['corpId'], $company_info['secret']);
    $url = "https://qyapi.weixin.qq.com/cgi-bin/media/upload?access_token=$access_token&type=image";
    $ch = curl_init();
    $minetype = 'image/png';
    $curl_file = curl_file_create($filename, $minetype);
    $postData = [
        'media' => $curl_file,
    ];
    curl_setopt($ch, CURLOPT_URL, $url);
    //curl结果不直接输出
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    
    //发送post 请求
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
    //允许 cURL 函数执行的最长秒数
    curl_setopt($ch, CURLOPT_TIMEOUT, 50);
    //不输出header 头信息
    curl_setopt($ch, CURLOPT_HEADER, 0);
    //不验证证书 信任任何证书
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    // 检查证书中是否设置域名,0不验证 0:不检查通用名称(CN)属性 1:检查通用名称属性是否存在  2:检查通用名称是否存在,是否与服务器的主机名称匹配
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    @curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    $res = curl_exec($ch);
    curl_close($ch);
    @unlink($filename);
    $upload_result = json_decode($res, true);
    die($upload_result['media_id']);
  • 相关阅读:
    25.Zabbix入门必备
    6.Ansible Roles角色实战
    5.Ansible Jinja2 模板
    4.Ansible Task控制
    3.Ansible varialbes实战
    2.Ansible Playbook剧本
    1.Ansible自动化管理工具
    网站架构面试题必备
    winsows CMD及Linux命令大全 欢迎补充
    Oracle查询表空间
  • 原文地址:https://www.cnblogs.com/yang-2018/p/15260968.html
Copyright © 2011-2022 走看看