* @throws \Exception
* @author liuml
* @DateTime 2018/12/21 15:52
public
function
curl
(
$url
,
$data
=
NULL
,
$format
=
'XML'
,
$timeOut
=
30
)
$ch
=
curl_init
(
)
;
curl_setopt
(
$ch
,
CURLOPT_RETURNTRANSFER
,
true
)
;
curl_setopt
(
$ch
,
CURLOPT_URL
,
$url
)
;
switch
(
$format
)
{
case
'XML'
:
$headers
=
[
"Content-Type:text/xml; charset=utf-8"
,
break
;
case
'FileUp'
:
$headers
=
[
"content-type:multipart/form-data"
,
break
;
default
:
$headers
=
[
"Content-Type:application/x-www-form-urlencoded; charset=utf-8"
,
break
;
if
(
$headers
)
{
curl_setopt
(
$ch
,
CURLOPT_HTTPHEADER
,
$headers
)
;
if
(
substr
(
$url
,
0
,
5
)
==
'https'
)
{
curl_setopt
(
$ch
,
CURLOPT_SSL_VERIFYPEER
,
false
)
;
curl_setopt
(
$ch
,
CURLOPT_SSL_VERIFYHOST
,
false
)
;
if
(
!
empty
(
$data
)
)
{
curl_setopt
(
$ch
,
CURLOPT_POST
,
true
)
;
curl_setopt
(
$ch
,
CURLOPT_POSTFIELDS
,
$data
)
;
curl_setopt
(
$ch
,
CURLOPT_CONNECTTIMEOUT
,
$timeOut
)
;
curl_setopt
(
$ch
,
CURLOPT_HEADER
,
false
)
;
$reponse
=
curl_exec
(
$ch
)
;
if
(
curl_errno
(
$ch
)
>
0
)
{
throw
new
\
Exception
(
$this
-
>
curlErrnoContrast
(
curl_errno
(
$ch
)
)
,
0
)
;
}
else
{
$httpStatusCode
=
curl_getinfo
(
$ch
,
CURLINFO_HTTP_CODE
)
;
if
(
200
!==
$httpStatusCode
)
{
throw
new
\
Exception
(
$reponse
,
$httpStatusCode
)
;
curl_close
(
$ch
)
;
return
$reponse
;
* curlErrnoContrast
* @param $code
* @return mixed
* @author liuml <[email protected]>
* @DateTime 2019/1/24 10:20
public
function
curlErrnoContrast
(
$code
)
$error_codes
=
[
'1'
=
>
'CURLE_UNSUPPORTED_PROTOCOL'
,
'2'
=
>
'CURLE_FAILED_INIT'
,
'3'
=
>
'CURLE_URL_MALFORMAT'
,
'4'
=
>
'CURLE_URL_MALFORMAT_USER'
,
'5'
=
>
'CURLE_COULDNT_RESOLVE_PROXY'
,
'6'
=
>
'CURLE_COULDNT_RESOLVE_HOST'
,
'7'
=
>
'CURLE_COULDNT_CONNECT'
,
'8'
=
>
'CURLE_FTP_WEIRD_SERVER_REPLY'
,
'9'
=
>
'CURLE_REMOTE_ACCESS_DENIED'
,
'11'
=
>
'CURLE_FTP_WEIRD_PASS_REPLY'
,
'13'
=
>
'CURLE_FTP_WEIRD_PASV_REPLY'
,
'14'
=
>
'CURLE_FTP_WEIRD_227_FORMAT'
,
'15'
=
>
'CURLE_FTP_CANT_GET_HOST'
,
'17'
=
>
'CURLE_FTP_COULDNT_SET_TYPE'
,
'18'
=
>
'CURLE_PARTIAL_FILE'
,
'19'
=
>
'CURLE_FTP_COULDNT_RETR_FILE'
,
'21'
=
>
'CURLE_QUOTE_ERROR'
,
'22'
=
>
'CURLE_HTTP_RETURNED_ERROR'
,
'23'
=
>
'CURLE_WRITE_ERROR'
,
'25'
=
>
'CURLE_UPLOAD_FAILED'
,
'26'
=
>
'CURLE_READ_ERROR'
,
'27'
=
>
'CURLE_OUT_OF_MEMORY'
,
'28'
=
>
'CURLE_OPERATION_TIMEDOUT'
,
'30'
=
>
'CURLE_FTP_PORT_FAILED'
,
'31'
=
>
'CURLE_FTP_COULDNT_USE_REST'
,
'33'
=
>
'CURLE_RANGE_ERROR'
,
'34'
=
>
'CURLE_HTTP_POST_ERROR'
,
'35'
=
>
'CURLE_SSL_CONNECT_ERROR'
,
'36'
=
>
'CURLE_BAD_DOWNLOAD_RESUME'
,
'37'
=
>
'CURLE_FILE_COULDNT_READ_FILE'
,
'38'
=
>
'CURLE_LDAP_CANNOT_BIND'
,
'39'
=
>
'CURLE_LDAP_SEARCH_FAILED'
,
'41'
=
>
'CURLE_FUNCTION_NOT_FOUND'
,
'42'
=
>
'CURLE_ABORTED_BY_CALLBACK'
,
'43'
=
>
'CURLE_BAD_FUNCTION_ARGUMENT'
,
'45'
=
>
'CURLE_INTERFACE_FAILED'
,
'47'
=
>
'CURLE_TOO_MANY_REDIRECTS'
,
'48'
=
>
'CURLE_UNKNOWN_TELNET_OPTION'
,
'49'
=
>
'CURLE_TELNET_OPTION_SYNTAX'
,
'51'
=
>
'CURLE_PEER_FAILED_VERIFICATION'
,
'52'
=
>
'CURLE_GOT_NOTHING'
,
'53'
=
>
'CURLE_SSL_ENGINE_NOTFOUND'
,
'54'
=
>
'CURLE_SSL_ENGINE_SETFAILED'
,
'55'
=
>
'CURLE_SEND_ERROR'
,
'56'
=
>
'CURLE_RECV_ERROR'
,
'58'
=
>
'CURLE_SSL_CERTPROBLEM'
,
'59'
=
>
'CURLE_SSL_CIPHER'
,
'60'
=
>
'CURLE_SSL_CACERT'
,
'61'
=
>
'CURLE_BAD_CONTENT_ENCODING'
,
'62'
=
>
'CURLE_LDAP_INVALID_URL'
,
'63'
=
>
'CURLE_FILESIZE_EXCEEDED'
,
'64'
=
>
'CURLE_USE_SSL_FAILED'
,
'65'
=
>
'CURLE_SEND_FAIL_REWIND'
,
'66'
=
>
'CURLE_SSL_ENGINE_INITFAILED'
,
'67'
=
>
'CURLE_LOGIN_DENIED'
,
'68'
=
>
'CURLE_TFTP_NOTFOUND'
,
'69'
=
>
'CURLE_TFTP_PERM'
,
'70'
=
>
'CURLE_REMOTE_DISK_FULL'
,
'71'
=
>
'CURLE_TFTP_ILLEGAL'
,
'72'
=
>
'CURLE_TFTP_UNKNOWNID'
,
'73'
=
>
'CURLE_REMOTE_FILE_EXISTS'
,
'74'
=
>
'CURLE_TFTP_NOSUCHUSER'
,
'75'
=
>
'CURLE_CONV_FAILED'
,
'76'
=
>
'CURLE_CONV_REQD'
,
'77'
=
>
'CURLE_SSL_CACERT_BADFILE'
,
'78'
=
>
'CURLE_REMOTE_FILE_NOT_FOUND'
,
'79'
=
>
'CURLE_SSH'
,
'80'
=
>
'CURLE_SSL_SHUTDOWN_FAILED'
,
'81'
=
>
'CURLE_AGAIN'
,
'82'
=
>
'CURLE_SSL_CRL_BADFILE'
,
'83'
=
>
'CURLE_SSL_ISSUER_ERROR'
,
'84'
=
>
'CURLE_FTP_PRET_FAILED'
,
'85'
=
>
'CURLE_RTSP_CSEQ_ERROR'
,
'86'
=
>
'CURLE_RTSP_SESSION_ERROR'
,
'87'
=
>
'CURLE_FTP_BAD_FILE_LIST'
,
'88'
=
>
'CURLE_CHUNK_FAILED'
,
return
$error_codes
[
$code
]
?
?
''
;
2、一般没有特殊需求用下面这个就行了
public function https_request($url, $data = null)
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
if(substr($url,0,5) == 'https'){
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, true);
if (!empty($data)){
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($curl);
curl_close($curl);
return $output;
本文实例讲述了php使用CURL模拟GET与POST向微信接口提交及获取数据的方法。分享给大家供大家参考,具体如下:
php CURL函数可以模仿用户进行一些操作,如我们可以模仿用户提交数据也可以模仿用户进行网站访问了,下面我们来介绍利用CURL模拟进行微信接口的GET与POST例子,例子非常的简单就两个:
Get提交获取数据
* @desc 获取access_token
* @return String access_token
function getAccessToken(){
$AppId = '1232assad13213123';
$AppSecret =
php编程中经常会用到用xml格式传送数据,如调用微信等第三方接口经常用到,这里演示下php以curl形式发送xml,并通过服务器接收一、发送xml数据 —— postXml.php<?php
//首先检测是否支持curl
if(!extension_loaded("curl")){
trigger_error("对不起,请开启curl功能模块!",E_USE...
public function ttt_json(){
$result = array("method" => null);
if($_SERVER['REQUEST_METHOD'] == 'GET' ? true : false){
$result = array("method" => "GET","data" =>
1 $xml_data ='<xml>
2 <return_code><![CDATA[SUCCESS]]></return_code>
3 <return_msg><![CDATA[OK]]></return_msg>
4 </xml>
7 ...
$url = "http://localhost/mytea_pc/weixin/warning/response.php";
$data = 'wxf8b4f85f3a794e771001错误描述transaction_id=335344535341393860740f8164781a303f4d5a944a2dfc68411a8c7e4fbeasha1';
$ch = curl_init()
php编程中经常会用到用xml格式传送数据,如调用微信等第三方接口经常用到,这里演示下php以curl形式发送xml,并通过服务器接收
一、发送xml数据 —— postXml.php
<?php
// 首先检测是否支持curl
if (!extension_loaded("curl")) {
trigger_error("对不起,请开启cur...