Provides various official and user-released code samples for reference. You are welcome to learn about http request encapsulation.
/*
* Http request tool
*/
/*
* Get method
*/
Function get ($ url, $ param = array ()){
If (! Is_array ($ param )){
Throw new Exception ("the parameter must be array ");
}
$ P = '';
Foreach ($ param as $ key => $ value ){
$ P = $ p. $ key. '='. $ value .'&';
}
If (preg_match ('/\? [\ D \ D] +/', $ url) {// matched? C
$ P = '&'. $ p;
} Else if (preg_match ('/\? $/', $ Url) {// matched? $
$ P = $ p;
} Else {
$ P = '? '. $ P;
}
$ P = preg_replace ('/& $/', '', $ p );
$ Url = $ url. $ p;
// Echo $ url;
$ Httph = curl_init ($ url );
Curl_setopt ($ httph, CURLOPT_SSL_VERIFYPEER, 0 );
Curl_setopt ($ httph, CURLOPT_SSL_VERIFYHOST, 1 );
Curl_setopt ($ httph, CURLOPT_RETURNTRANSFER, 1 );
Curl_setopt ($ httph, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0 )");
Curl_setopt ($ httph, CURLOPT_RETURNTRANSFER, 1 );
Curl_setopt ($ httph, CURLOPT_HEADER, 1 );
$ Rst = curl_exec ($ httph );
Curl_close ($ httph );
Return $ rst;
}
/*
* Post method
*/
Function post ($ url, $ param = array ()){
If (! Is_array ($ param )){
Throw new Exception ("the parameter must be array ");
}
$ Httph = curl_init ($ url );
Curl_setopt ($ httph, CURLOPT_SSL_VERIFYPEER, 0 );
Curl_setopt ($ httph, CURLOPT_SSL_VERIFYHOST, 1 );
Curl_setopt ($ httph, CURLOPT_RETURNTRANSFER, 1 );
Curl_setopt ($ httph, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0 )");
Curl_setopt ($ httph, CURLOPT_POST, 1); // set it to POST
Curl_setopt ($ httph, CURLOPT_POSTFIELDS, $ param );
Curl_setopt ($ httph, CURLOPT_RETURNTRANSFER, 1 );
Curl_setopt ($ httph, CURLOPT_HEADER, 1 );
$ Rst = curl_exec ($ httph );
Curl_close ($ httph );
Return $ rst;
}
Http.zip (680 B download: 157 times)
AD: truly free, domain name + VM + enterprise mailbox = 0 RMB