030.PHP模拟HTTP接口请求 2020-03-18 Web全栈架构►PHP 接口请求 本文总阅读量次 模拟 HTML 表单提交1234567891011121314151617181920212223242526// 模拟 POST 请求function httpRequstPost($url,$data){ if(!is_array($data)){ throw new Exception("无法识别$data的数据类型"); } $FormString = "<body onLoad=\"document.actform.submit()\">正在处理,请稍候.....................<form id=\"actform\" name=\"actform\" method=\"post\" action=\"" . $url . "\">"; foreach($data as $key => $value){ $FormString .="<input name=\"" . $key . "\" type=\"hidden\" value='" . $value . "'>\r\n"; } $FormString .="</form></body>"; return $FormString;}// 模拟get请求function getHtml($url, $data){ if (!is_array($data)) { throw new Exception("无法识别的数据类型【PostArry】"); } $FormString = "<body onLoad=\"document.actform.submit()\">正在处理,请稍候.....................<form id=\"actform\" name=\"actform\" method=\"get\" action=\"" . $url . "\">"; foreach ($data as $key => $value) { $FormString .= "<input name=\"" . $key . "\" type=\"hidden\" value='" . $value . "'>\r\n"; } $FormString .= "</form></body>"; return $FormString;} CURL 模拟表单的post提交1234567891011121314151617181920212223function httpRequstPost($url,$data){ $headers = array('Content-Type: application/x-www-form-urlencoded'); $curl = curl_init(); // 启动一个CURL会话 curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查 curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); // 从证书中检查SSL加密算法是否存在 curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器 curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转 curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求 curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data)); // Post提交的数据包 curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环 curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回 curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); $result = curl_exec($curl); // 执行操作 if (curl_errno($curl)) { echo 'Errno'.curl_error($curl);//捕抓异常 } curl_close($curl); // 关闭CURL会话 return $result;} file_get_contens()函数模拟get请求1234$url='接口地址';$result=file_get_contents($url);$return result; // 返回响应数据 以上内容仅供参考,转载请注明出处。 031.Mybatis概要与Springboot整合 029.JDBC事务处理