PHP WeChat How to get temporary footage (with code)

Source: Internet
Author: User
This article brings you the content is about PHP to get temporary material (with code), there is a certain reference value, the need for friends can refer to, I hope to help you.

Note: 1: Media files are stored in the background for 3 days, that is, after 3 days, the media_id expires.

2: Temporary footage media_id is reusable.

If it is php5.3 below the path path needs to take @, plus the absolute path of the text, more than 5.3 of the version needs to use the new Curlfile () class to obtain an absolute address

$path = new Curlfile (Realpath (' g:/xampp/htdocs/wx/app/zan.jpg ')), $path = $path->name;//absolute path $type = ' images ';// Thumb$res = $this->upload_media (' image ', $path);//Get media_id of footage, valid for 3 days $media_id = $res->media_id;// The following is the get temporary footage Url$url = $this->get_media ($media _id);//Get URL to temporary footage public function Upload_media ($type, $path) {$ url = ' https://api.weixin.qq.com/cgi-bin/media/upload?access_token= '. $this->get_access_token (). ' &type= '.        $type;        $res = $this->upload ($url, Array (' media ' = ' @ '. $path));    Determines whether a successful return $res is invoked;  } Public Function Get_media ($media _id) {return ' https://api.weixin.qq.com/cgi-bin/media/get?access_token= ' . $this->get_access_token (). ' &media_id= '.    $media _id; }/* Upload pictures.          Graphic Dedicated */public static function upload ($url, $filedata) {$curl = Curl_init (); if (class_exists ('/curlfile ')) {//php5.5 differs from Curlopt_safe_upload's default value in php5.6 curl_setopt ($curl, CurloPt_safe_upload, True); } else {if (defined (' Curlopt_safe_upload ')) {curl_setopt ($curl, Curlopt_safe_upload,              FALSE);          }} curl_setopt ($curl, Curlopt_url, $url);          curl_setopt ($curl, Curlopt_ssl_verifypeer, FALSE);          curl_setopt ($curl, Curlopt_ssl_verifyhost, FALSE);              if (! empty ($filedata)) {curl_setopt ($curl, Curlopt_post, 1);          curl_setopt ($curl, Curlopt_postfields, $filedata);          } curl_setopt ($curl, Curlopt_returntransfer, 1);          $output = curl_exec ($curl);          Curl_close ($curl);                return $output; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.