curl请求方法
/** * CURL 提交请求数据 * @param string $url 请求URL * @param string $postData 请求发送的数据 * @param int $port 请求端口 * @param int $timeout 超时时间 * @param array $headers 请求头信息 * @return bool|mixed */ function curl_post($url, $postData, $port = 80, $timeout = 25, $headers = []) { //超时时间处理 $timeout = intval($timeout); $timeout = $timeout <= 0 ? 25 : $timeout; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_PORT, $port); curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HEADER, 0); if ((is_array($headers) || is_object($headers)) && count($headers)) { curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); } curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $postData); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); $res = curl_exec($ch); //错误处理 $errCode = curl_errno($ch); if ($errCode > 0) { curl_close($ch); return false; } else { //获取HTTP码 $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); if ($httpCode != 200) { curl_close($ch); return false; } else { curl_close($ch); return $res; } } }
file_get_contents请求方法
/** * 统一封装的file_get_contents * @param string $url 请求url * @param integer $timeout 超时时间 * @param array $header 请求头部 * @return */ function pft_file_get_contents($url, $timeout = 10, $header = []){ $url = strval($url); $timeout = intval($timeout); $timeout = $timeout <= 0 ? 10 : $timeout; $contextOptions = [ 'http' => ['timeout' => $timeout] ]; if($header) { $contextOptions['http']['header'] = $header; } $context = stream_context_create($contextOptions); $res = file_get_contents($url, false, $context); return $res; }
soap请求方法
/** * 统一封装的SOAP客户端封装,有些系统还在使用soap协议提供接口 * $soapClient = new PftSoapClient('xxx.wsdl'); * $soapClient->setTimeout(25); * $soapClient->getMyMoney($params); */ class PftSoapClient extends \SoapClient { //超时的时间 private $timeout = 0; //设置超时时间 public function setTimeout($timeout) { $timeout = intval($timeout); $timeout = $timeout <= 0 ? 25 : $timeout; $this->timeout = $timeout; } //请求接口 public function __doRequest($request, $location, $action, $version, $oneWay = FALSE) { if ($this->timeout <= 0) { //使用默认的方式 $res = parent::__doRequest($request, $location, $action, $version, $oneWay); } else { //使用添加了超时的方式 $socketTime = ini_get('default_socket_timeout'); ini_set('default_socket_timeout', $this->timeout); $res = parent::__doRequest($request, $location, $action, $version, $oneWay); ini_set('default_socket_timeout', $socketTime); } return $res; } }
宠物免费领养小程序,欢迎来踩