php的http请求有很多种方式,可以使用 文件操作函数,也可以使用curl 扩展(php配置中需要启用curl扩展)
file_get_contents版本:

  1. <?php
  2. /**
  3. * 发送post请求
  4. * @param string $url 请求地址
  5. * @param array $post_data post键值对数据
  6. * @return string
  7. */
  8. function send_post($url, $post_data) {
  9. $postdata = http_build_query($post_data);
  10. $options = array(
  11. 'http' => array(
  12. 'method' => 'POST',
  13. 'header' => 'Content-type:application/x-www-form-urlencoded',
  14. 'content' => $postdata,
  15. 'timeout' => 15 * 60 // 超时时间(单位:s)
  16. )
  17. );
  18. $context = stream_context_create($options);
  19. $result = file_get_contents($url, false, $context);
  20. return $result;
  21. }

使用如下:

  1. $post_data = array(
  2. 'username' => 'xxx',
  3. 'password' => '000'
  4. );
  5. send_post('http://blog.sohu.com', $post_data);

PS: 在发送http请求的时候,如果服务器处理请求时间超长,本地php会中断请求(超时中断)需要把 http 请求时间限制设置大一些(timeout)

Socket版本:

  1. /**
  2. * Socket版本
  3. * 使用方法:
  4. * $post_string = "app=socket&amp;version=beta";
  5. * request_by_socket('blog.sohu.com', '/restServer.php', $post_string);
  6. */
  7. function request_by_socket($remote_server,$remote_path,$post_string,$port = 80,$timeout = 30) {
  8. $socket = fsockopen($remote_server, $port, $errno, $errstr, $timeout);
  9. if (!$socket) die("$errstr($errno)");
  10. fwrite($socket, "POST $remote_path HTTP/1.0");
  11. fwrite($socket, "User-Agent: Socket Example");
  12. fwrite($socket, "HOST: $remote_server");
  13. fwrite($socket, "Content-type: application/x-www-form-urlencoded");
  14. fwrite($socket, "Content-length: " . strlen($post_string) + 8 . "");
  15. fwrite($socket, "Accept:*/*");
  16. fwrite($socket, "");
  17. fwrite($socket, "mypost=$post_string");
  18. fwrite($socket, "");
  19. $header = "";
  20. while ($str = trim(fgets($socket, 4096))) {
  21. $header .= $str;
  22. }
  23. $data = "";
  24. while (!feof($socket)) {
  25. $data .= fgets($socket, 4096);
  26. }
  27. return $data;
  28. }

Curl版本:

  1. /**
  2. * Curl版本
  3. * 使用方法:
  4. * $post_string = "app=request&version=beta";
  5. * request_by_curl('http://blog.sohu.com/restServer.php', $post_string);
  6. */
  7. function request_by_curl($remote_server, $post_string) {
  8. $ch = curl_init();
  9. curl_setopt($ch, CURLOPT_URL, $remote_server);
  10. curl_setopt($ch, CURLOPT_POSTFIELDS, 'mypost=' . $post_string);
  11. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  12. curl_setopt($ch, CURLOPT_USERAGENT, "sohu.com's CURL Example beta");
  13. $data = curl_exec($ch);
  14. curl_close($ch);
  15. return $data;
  16. }

Curl版本(POST/GET)

  1. /**
  2. * 发送HTTP请求
  3. *
  4. * @param string $url 请求地址
  5. * @param string $method 请求方式 GET/POST
  6. * @param string $refererUrl 请求来源地址
  7. * @param array $data 发送数据
  8. * @param string $contentType
  9. * @param string $timeout
  10. * @param string $proxy
  11. * @return boolean
  12. */
  13. function send_request($url, $data, $refererUrl = '', $method = 'GET', $contentType = 'application/json', $timeout = 30, $proxy = false) {
  14. $ch = null;
  15. if('POST' === strtoupper($method)) {
  16. $ch = curl_init($url);
  17. curl_setopt($ch, CURLOPT_POST, 1);
  18. curl_setopt($ch, CURLOPT_HEADER,0 );
  19. curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
  20. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  21. curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
  22. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  23. if ($refererUrl) {
  24. curl_setopt($ch, CURLOPT_REFERER, $refererUrl);
  25. }
  26. if($contentType) {
  27. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:'.$contentType));
  28. }
  29. if(is_string($data)){
  30. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  31. } else {
  32. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
  33. }
  34. } else if('GET' === strtoupper($method)) {
  35. if(is_string($data)) {
  36. $real_url = $url. (strpos($url, '?') === false ? '?' : ''). $data;
  37. } else {
  38. $real_url = $url. (strpos($url, '?') === false ? '?' : ''). http_build_query($data);
  39. }
  40. $ch = curl_init($real_url);
  41. curl_setopt($ch, CURLOPT_HEADER, 0);
  42. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:'.$contentType));
  43. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  44. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  45. if ($refererUrl) {
  46. curl_setopt($ch, CURLOPT_REFERER, $refererUrl);
  47. }
  48. } else {
  49. $args = func_get_args();
  50. return false;
  51. }
  52. if($proxy) {
  53. curl_setopt($ch, CURLOPT_PROXY, $proxy);
  54. }
  55. $ret = curl_exec($ch);
  56. $info = curl_getinfo($ch);
  57. $contents = array(
  58. 'httpInfo' => array(
  59. 'send' => $data,
  60. 'url' => $url,
  61. 'ret' => $ret,
  62. 'http' => $info,
  63. )
  64. );
  65. curl_close($ch);
  66. return $ret;
  67. }

分类: PHP

标签:   php