<?php
class
Wchat
{
private
$app_id
= 'wx444444444444';
private
$app_secret
= '77777777';
private
$state
='aaaa';
public
function
get_authorize_url(
$redirect_uri
= '',
$state
= '')
{
$redirect_uri
= urlencode(
$redirect_uri
);
return
"https:
}
public
function
getOpenid(
$turl
)
{
if
(!isset(
$_GET
['code'])){
$url
=
$this
->get_authorize_url(
$turl
,
$this
->state);
Header("Location:
$url
");
exit
();
}
else
{
$code
=
$_GET
['code'];
$access_info
=
$this
->get_access_token(
$code
);
return
$access_info
;
}
}
public
function
get_access_token(
$code
= '')
{
$appid
=
$this
->app_id;
$appsecret
=
$this
->app_secret;
$token_url
= "https:
$token_data
=
$this
->http(
$token_url
);
if
(
$token_data
[0] == 200)
{
$ar
=json_decode(
$token_data
[1], TRUE);
return
$ar
;
}
return
$token_data
[1];
}
public
function
http(
$url
,
$method
='',
$postfields
= null,
$headers
=
array
(),
$debug
= false)
{
$ci
= curl_init();
curl_setopt(
$ci
, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt(
$ci
, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt(
$ci
, CURLOPT_TIMEOUT, 30);
curl_setopt(
$ci
, CURLOPT_RETURNTRANSFER, true);
switch
(
$method
) {
case
'POST':
curl_setopt(
$ci
, CURLOPT_POST, true);
if
(!
empty
(
$postfields
)) {
curl_setopt(
$ci
, CURLOPT_POSTFIELDS,
$postfields
);
$this
->postdata =
$postfields
;
}
break
;
}
curl_setopt(
$ci
, CURLOPT_URL,
$url
);
curl_setopt(
$ci
, CURLOPT_HTTPHEADER,
$headers
);
curl_setopt(
$ci
, CURLINFO_HEADER_OUT, true);
$response
= curl_exec(
$ci
);
$http_code
= curl_getinfo(
$ci
, CURLINFO_HTTP_CODE);
if
(
$debug
) {
echo
"=====post data======\r\n";
var_dump(
$postfields
);
echo
'=====info=====' . "\r\n";
print_r(curl_getinfo(
$ci
));
echo
'=====
$response
=====' . "\r\n";
print_r(
$response
);
}
curl_close(
$ci
);
return
array
(
$http_code
,
$response
);
}
}
?>