Blogger Information
Blog 11
fans 0
comment 1
visits 13881
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP的3种请求方式 GET POST 伪异步POST
Alfred的学习笔记
Original
1024 people have browsed it
  1. /*
  2. * 发起POST网络提交
  3. * @params string $url : 网络地址
  4. * @params json $data : 发送的json格式数据
  5. */
  6. static public function https_post($url, $data) {
  7. $curl = curl_init();
  8. curl_setopt($curl, CURLOPT_URL, $url);
  9. if (!empty($data)) {
  10. curl_setopt($curl, CURLOPT_POST, 1);
  11. curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  12. }
  13. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  14. $output = curl_exec($curl);
  15. curl_close($curl);
  16. return $output;
  17. }
  18. /*
  19. * 发起GET网络提交
  20. * @params string $url : 网络地址
  21. */
  22. static public function https_get($url) {
  23. $curl = curl_init();
  24. curl_setopt($curl, CURLOPT_URL, $url);
  25. curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
  26. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
  27. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
  28. curl_setopt($curl, CURLOPT_HEADER, FALSE);
  29. curl_setopt($curl, CURLOPT_TIMEOUT, 60);
  30. if (curl_errno($curl)) {
  31. return 'Errno' . curl_error($curl);
  32. } else {
  33. $result = curl_exec($curl);
  34. }
  35. curl_close($curl);
  36. return $result;
  37. }
  38. /**
  39. * 异步post请求
  40. * @param type $host 域名 www.baidu.com
  41. * @param type $url 请求方法链接:/party.php/api/####
  42. * @param type $param 参数数组
  43. */
  44. static public function http_post_asynchronous_request($host = '', $url = '', $param = array()) {
  45. $host = isset($host) ? $host : 'www.baidu.com';
  46. $query = isset($param) ? http_build_query($param) : '';
  47. $port = 80;
  48. $errno = 0;
  49. $errstr = '';
  50. $timeout = 10;
  51. $fp = fsockopen($host, $port, $errno, $errstr, $timeout);
  52. $out = "POST " . $url . " HTTP/1.1\r\n";
  53. $out .= "host:" . $host . "\r\n";
  54. $out .= "content-length:" . strlen($query) . "\r\n";
  55. $out .= "content-type:application/x-www-form-urlencoded\r\n";
  56. $out .= "connection:close\r\n";
  57. $out .= $query;
  58. fputs($fp, $out);
  59. usleep(20000); //避免nginx服务器链接断开无法 报499 链接失效
  60. fclose($fp);
  61. }
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post