scene
About micro-channel operation ends, often meet, to generate two-dimensional code chrysanthemum, invite your friends to help, or business operations Offered like, then, you need to generate a two-dimensional code, to the front end, then, how how to use PHP generate it? As follows:
Step 1: Generate access_token, ready to work
/**
* Get access_token value
* @return string $access_token
*/
public function get_access_token()
{
$appConfig = [
'app_id' => 'wxd92dsdfsdfec5efc',
'secret' => 'ec18798a8dsdfsdfsdfa7d29106'
];
$url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appConfig['app_id'].'&secret='.$appConfig['secret'];
$ Ch = curl_init (); // create the handle
curl_setopt ($ ch, CURLOPT_URL, $ url); // get the data through url
curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1); // get information returned as a stream file
curl_setopt ($ ch, CURLOPT_SSL_VERIFYPEER, false); // skip verification certificate
curl_setopt ($ ch, CURLOPT_SSL_VERIFYHOST, false); // check the SSL encryption certificate is
$output = json_decode(curl_exec($ch));
$access_token = $output->access_token;
curl_close($ch);
return $access_token;
}
Step two: business operations, to be associated traffic parameters, generated into "two-dimensional code in the picture."
/**
* Generate [invite friends to join the league | Friends help boost the two-dimensional code]
* @param $type
* @param $userid
* @param $usg_id
* @return array
*/
public function product_step_active_QRcode($type, $userid, $usg_id)
{
// Get redis_access_token value of redis
$this->load->service('order_service');
$access_token = $this->get_access_token();
if(!$access_token){
$result['data'] = [];
return $result;
}
// key parameter
$scene = $usg_id.'_'.$userid.'_'.$type;
// Image Name
$imgName = 'step_active_QRcode_'.$scene;
// request Picture Address
$url = 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token='.$access_token;
// request parameters
$request = [];
$request['scene'] = $scene;
$ Request [ 'page'] = 'pages / septemberActivity / index'; // jump page scan code
$ Request [ 'width'] = 280; // applet sized px
$ Request [ 'is_hyaline'] = TRUE; // need for transparent background
// request data from the end of the micro channel
$Qr_code = $this->curl_post_weixin($url, $request);
// read data obtained in a temporary picture
file_put_contents('/tmp/'.$imgName.'.png', $Qr_code);
// The image file into base64
$img_string = $this->fileToBase64('/tmp/'.$imgName.'.png');
// delete temporary files
unlink('/tmp/'.$imgName.'.png');
// return data
$result['data'] = $img_string;
return $result;
}
The third step: cURL manner required by the service request parameters to the micro address to the image communication terminal, returns the two-dimensional code data
/**
* CURL POST data to the micro-channel mode
* @Param string $ url address request
* @Param array $ data to send data
* @return resource
*/
public function curl_post_weixin($url, $data)
{
if($url && count($data)){
$headers = ['Content-Type:application/json'];
$ch = curl_init();
curl_setopt ($ ch, CURLOPT_HTTPHEADER, $ headers); // key points
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
$res = curl_exec($ch);
curl_close($ch);
return $res;
}
}
Step 4: micro-channel end of the return and save to local temporary image file, to 64-bit code (for convenience to the front)
/**
* Local file transfer base64
* @param $image_file
* @return mixed
*/
function fileToBase64 ($image_file) {
$base64_image = '';
$image_info = getimagesize($image_file);
$image_data = fread(fopen($image_file, 'r'), filesize($image_file));
$base64_image = 'data:' . $image_info['mime'] . ';base64,' . chunk_split(base64_encode($image_data));
return $base64_image;
}
to sum up
Generating micro-channel two-dimensional code picture, the key point is: deal with the familiarity of the micro-letter code for common business processes, use cURL functions, as well as picture files. Mastered these three points, generating micro-channel two-dimensional code are not any hard up! All in all, the programming this road is to practice more, more, think more summary, so, in order to progress! Long way to go, to seek!