PFinal\Wechat\Support\Json::encode PHP Method

encode() public static method

将数据编码为json,用于请求微信平台服务器
public static encode ( $data ) : string
$data
return string
    public static function encode($data)
    {
        if (version_compare(PHP_VERSION, '5.4.0', '<')) {
            $str = json_encode($data);
            $str = preg_replace_callback('#\\\\u([0-9a-f]{4})#i', function ($matchs) {
                return iconv('UCS-2BE', 'UTF-8', pack('H4', $matchs[1]));
            }, $str);
            return str_replace('\\/', '/', $str);
        }
        return json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
        //php 5.4.0
    }

Usage Example

Ejemplo n.º 1
0
 /**
  * 新增永久素材 媒体文件类型别有图片(image)、语音(voice) 、视频(video)和缩略图(thumb)
  *
  * @param string $filename
  * @param string $type
  * @param string $title 视频素材的标题 只对类型为video有效
  * @param string $introduction 视频素材的描述 只对类型为video有效
  * @return array
  */
 public static function uploadFile($filename, $type, $title = null, $introduction = null)
 {
     $filename = realpath($filename);
     if (class_exists('\\CURLFile')) {
         $data['media'] = new \CURLFile($filename);
     } else {
         $data['media'] = '@' . $filename;
     }
     $data['type'] = $type;
     if ($type === 'video') {
         $data['description'] = Json::encode(array('title' => $title, 'introduction' => $introduction));
     }
     $url = 'https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=ACCESS_TOKEN';
     return parent::request($url, $data, false);
 }
All Usage Examples Of PFinal\Wechat\Support\Json::encode