No material management to obtain public list - Tutorial

Thinking
1. The appid access_token acquired public number and Secret;
2. Obtain a list of material data management, and related parameters according to access_token
Direct upload code, comments, very simple.

? < PHP
 / * 
 * get access_token 
 * grant_type get access_token fill client_credential 
 * AppID third-party users only certificate 
 * secret key third-party users only certificate that appsecret 
* * / 
$ AppID = " ** " ; 
$ Secret = " ** " ; 
$ the access_token = get_access_token (AppID $, $ Secret);
 // Print" <div> debug credentials </ div> <br /> "$ the access_token.." "; 
$ returndata = get_article_list ( " News " , " 0 " , " 1 " , the access_token $);
print "<div> data returned " . $ returndata. " </ div> <br /> " ; 
function get_access_token (AppID $, $ Secret) 
{ 
    $ URL = " https://api.weixin.qq.com/cgi- ? bin / token grant_type = client_credential & AppID = " $ AppID.. " & Secret = " $ Secret.. " " ; 
    $ Response = file_get_contents ($ URL);
     // returns the data formats: {" access_token ":" ACCESS_TOKEN "," expires_in ": 7200}
     // expires_in: document valid time 
    $ of json_decode RES = ($ Response, to true );
    return $res['access_token' ]; 
}; 
/ * 
    * Get a list of articles 
    * type type of material: picture (image), video (video), voice (voice), graphics (News) 
    * offset the offset from the beginning of the return of all the material, 0 indicates a return from the first material 
    * count the number of returned material, ranging between 1 and 20 is 
    * / 
 function get_article_list ($ type, $ offset, $ count, the access_token $) 
{ 
    // HTTPS: //api.weixin .qq.com / cgi-bin / Material / batchget_material the access_token = ACCESS_TOKEN? 

    $ url = " https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token= " $ the access_token;. 
    $ the Data = ' { "type": " ' .. $ type ' ", "offset": " ' . $ offset.'"," COUNT ":" ' $ COUNT.. ' "} ' ;
     // return data 
    $ Response = get_response_post (URL $, $ Data);
     return $ Response; 

}; 
 function get_response_post (URL $, $ Data) 
{ 
    curl $ = curl_init ($ URL); 
    curl_setopt ($ curl, CURLOPT_HEADER, 0 ); // filter head 
    curl_setopt ($ curl, CURLOPT_RETURNTRANSFER, . 1 ); // acquired information returned as stream file, instead of being output . 
    curl_setopt ($ curl, CURLOPT_POST, to true ); // POST data transmission
    curl_setopt($curl,CURLOPT_POSTFIELDS,$data);// post传输数据
    $responseText = curl_exec($curl);
    curl_close($curl);
    return $responseText;
};
?>

 

Guess you like

Origin www.cnblogs.com/bluealine/p/11983356.html