This article mainly introduces the PHP HTTP request class, has a certain reference value, now share to everyone, the need for friends can refer to
<?phpnamespace App\lib;class lib_http{//initiates a network request public function call ($url, $args = null, $method = "POST", $testflag = 0, $timeout =, $headers = Array ()) {/*{{{*/lib_log::set_log (' http ', $url); $ret = false; $i = 0; while ($ret = = = False) {if ($i > 1) break; if ($i > 0) {sleep (1); } $ret = $this->callonce ($url, $args, $method, False, $timeout, $headers); $i + +; } return $ret; }/*}}}*/Private Function Callonce ($url, $args = null, $method = "POST", $withCookie = False, $timeout = Curl_timeout, $headers = Array ()) {/*{{{*/$ch = Curl_init (); if ($method = = "Post") {$data = $this->convert ($args); curl_setopt ($ch, Curlopt_postfields, $data); curl_setopt ($ch, Curlopt_post, 1); } else {$data = $this->convert ($args); if ($data) {if (Stripos ($URL, "?") > 0) {$url. = "& $data"; } else {$url. = "$data"; }}} curl_setopt ($ch, Curlopt_url, $url); curl_setopt ($ch, Curlopt_timeout, $timeout); curl_setopt ($ch, Curlopt_returntransfer, 1); curl_setopt ($ch, Curlopt_header, true); if (!empty ($headers)) {curl_setopt ($ch, Curlopt_httpheader, $headers); } if ($withCookie) {curl_setopt ($ch, Curlopt_cookiejar, $_cookie); } $r = Curl_exec ($ch); Curl_close ($ch); return $r; }/*}}}*/Private Function convert (& $args) {/*{{{*/$data = '; if (Is_array ($args)) {foreach ($args as $key = + $val) {if (Is_array ($val)) { foreach ($val as $k = = $v) {$data. = $key. ' ['. $k. ']=' . Rawurlencode ($v). ' & '; }} else { $data. = "$key =". Rawurlencode ($val). "&"; }} return Trim ($data, "&"); } return $args; }/*}}}*/}