1. 程式人生 > >php 網路請求 get請求和post請求

php 網路請求 get請求和post請求

程式碼記錄

<?php
header('content-type:application:json;charset=utf8'); 
header('Access-Control-Allow-Origin:*'); 
//header('Access-Control-Allow-Methods:POST'); 
header('Access-Control-Allow-Headers:x-requested-with,content-type');
	//訪問小程式獲取二維碼圖片試試,聽說是二進位制圖片
	qd_code_dsh();
	//多店鋪二維碼  一店一碼
	function qd_code_dsh(){
		$arr = array(
			 'path' => '/pages/index/index?dsh_id=1',
			 'width' => 430,
			 'scene' => 0
			);
		
		$path = json_encode($arr);
		
	 $post_data=$path;
	   $url="https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=".get_accessToken();
	   //$url="https://api.weixin.qq.com/wxa/getwxacode?access_token=".get_accessToken();
	  
	   $result=api_notice_increment($url,$post_data);
		
	   echo $result;
	 
	}


	function api_notice_increment($url, $data){
		$ch = curl_init();
		$header = "Accept-Charset: utf-8";
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
		curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
		curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		$tmpInfo = curl_exec($ch);
		//     var_dump($tmpInfo);
		//    exit;
		if (curl_errno($ch)) {
		  return false;
		}else{
		  // var_dump($tmpInfo);
		  return $tmpInfo;
		}
	  }


    /* 呼叫微信api,獲取access_token,有效期7200s -xzz0704 */
    function get_accessToken(){
        /* 直接返回access_token */
        $result = curl_get_https("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wxdcdbc3696ede3ec5&secret=b57a681d90f5d8552c1486831241c950");
		   $res = json_decode($result,true);   //json字串轉陣列
            if($res){
                return $res['access_token'];
            }else{
                return 'api return error';
            }
    }
    
    function curl_get_https($url){
		$curl = curl_init(); // 啟動一個CURL會話
		curl_setopt($curl, CURLOPT_URL, $url);
		curl_setopt($curl, CURLOPT_HEADER, 0);
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 跳過證書檢查
		//curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, true);  // 從證書中檢查SSL加密演算法是否存在
		$tmpInfo = curl_exec($curl);     //返回api的json物件
		//關閉URL請求
		curl_close($curl);
		return $tmpInfo;    //返回json物件
	}
?>