三个方法演示

方法1 — file_get_contents

  1. #$url = 'http://sports.qq.com/';
  2. // 发起请求 方案1 不推荐用 对https支持不好,会给我们的服务器添加压力,因为没有超时时间,一直在等待
  3. #$data = file_get_contents($url);

方法2 — fsockopen

缺点:麻烦。
优点:可控性比较强,可以任意加上请求头的参数

  1. // 方案2 不推荐
  2. // 域名 端口 错误码 错误信息 超时时间
  3. /*$data = '';
  4. $fp = fsockopen("localhost", 8080, $errno, $errstr, 30);
  5. if (!$fp) {
  6. echo "$errstr ($errno)<br />\n";
  7. } else {
  8. $out = "GET /abc.html HTTP/1.1\r\n";
  9. $out .= "Host: localhost\r\n";
  10. $out .= "User-Agent: MSIE\r\n";
  11. $out .= "Connection: Close\r\n\r\n";
  12. // 发送请求
  13. fwrite($fp, $out);
  14. while (!feof($fp)) {
  15. // 接受
  16. $data .= fgets($fp, 128);
  17. }
  18. // 关闭资源
  19. fclose($fp);
  20. }*/

方法3 — curl (推荐)

curl是扩展,需要我们在php.ini文件中开启的

get和post

下面代码中 有get和post的例子。还包含https的情况。

  1. $url = 'https://wx.1314000.cn/';
  2. #$url = 'http://localhost:8080/abc.html';
  3. // 文案3 推荐 curl是扩展,需要我们在php.ini文件中开启的
  4. function http_get($url){
  5. // 初始化
  6. $ch = curl_init();
  7. // 相关设置
  8. # 设置请求的URL地址
  9. curl_setopt($ch,CURLOPT_URL,$url);
  10. # 请求头关闭
  11. curl_setopt($ch,CURLOPT_HEADER,0);
  12. # 请求的得到的结果不直接输出,而是以字符串结果返回 必写
  13. curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
  14. # 设置请求的超时时间 单位秒
  15. curl_setopt($ch,CURLOPT_TIMEOUT,30);
  16. # 设置浏览器型号
  17. curl_setopt($ch,CURLOPT_USERAGENT,'MSIE001');
  18. # 证书不检查
  19. curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,0);
  20. curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,0);
  21. // 发起请求
  22. $data = curl_exec($ch);
  23. // 有没有发生异常
  24. if(curl_errno($ch) > 0){
  25. // 把错误发送给客户端
  26. echo curl_error($ch);
  27. $data = '';
  28. }
  29. // 关闭请求
  30. curl_close($ch);
  31. return $data;
  32. }
  33. function http_post($url,$ret){
  34. // 初始化
  35. $ch = curl_init();
  36. // 相关设置
  37. # 设置请求的URL地址
  38. curl_setopt($ch,CURLOPT_URL,$url);
  39. # 请求头关闭
  40. curl_setopt($ch,CURLOPT_HEADER,0);
  41. # 请求的得到的结果不直接输出,而是以字符串结果返回 必写
  42. curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
  43. # 设置请求的超时时间 单位秒
  44. curl_setopt($ch,CURLOPT_TIMEOUT,30);
  45. # 设置浏览器型号
  46. curl_setopt($ch,CURLOPT_USERAGENT,'MSIE001');
  47. # 证书不检查
  48. curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,0);
  49. curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,0);
  50. # 设置为post请求
  51. # 开启post请求
  52. curl_setopt($ch,CURLOPT_POST,1);
  53. # post请求的数据
  54. curl_setopt($ch,CURLOPT_POSTFIELDS,$ret);
  55. // 发起请求
  56. $data = curl_exec($ch);
  57. // 有没有发生异常
  58. if(curl_errno($ch) > 0){
  59. // 把错误发送给客户端
  60. echo curl_error($ch);
  61. $data = '';
  62. }
  63. // 关闭请求
  64. curl_close($ch);
  65. return $data;
  66. }
  67. #echo http_post('http://localhost:8080/post.php',['id'=>1,'name'=>'张三']);

文件上传

这个需要稍微讲下,第一次看还是比较懵逼的
比如,我要通过crul上传一个图片,最后一个$file 是一个图片的绝对路径。然后将这个图片路径给封装成一个图片资源放到$res 中请求即可。类比下 表单提交 就明白了。

  1. function http_post_file($url,$ret,$file=''){
  2. if (!empty($file)) { // 有文件上传
  3. # php5.5之前 '@'.$file;就可以进地文件上传
  4. # $ret['pic'] = '@'.$file;
  5. # php5.6之后用此方法
  6. $ret['pic'] = new CURLFile($file);
  7. }
  8. // 初始化
  9. $ch = curl_init();
  10. // 相关设置
  11. # 设置请求的URL地址
  12. curl_setopt($ch,CURLOPT_URL,$url);
  13. # 请求头关闭
  14. curl_setopt($ch,CURLOPT_HEADER,0);
  15. # 请求的得到的结果不直接输出,而是以字符串结果返回 必写
  16. curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
  17. # 设置请求的超时时间 单位秒
  18. curl_setopt($ch,CURLOPT_TIMEOUT,30);
  19. # 设置浏览器型号
  20. curl_setopt($ch,CURLOPT_USERAGENT,'MSIE001');
  21. # 证书不检查
  22. curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,0);
  23. curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,0);
  24. # 设置为post请求
  25. # 开启post请求
  26. curl_setopt($ch,CURLOPT_POST,1);
  27. # post请求的数据
  28. curl_setopt($ch,CURLOPT_POSTFIELDS,$ret);
  29. // 发起请求
  30. $data = curl_exec($ch);
  31. // 有没有发生异常
  32. if(curl_errno($ch) > 0){
  33. // 把错误发送给客户端
  34. echo curl_error($ch);
  35. $data = '';
  36. }
  37. // 关闭请求
  38. curl_close($ch);
  39. return $data;
  40. }
  41. # 绝对路径
  42. $file = __DIR__.'/1.jpg';
  43. echo http_post_file('http://localhost:8080/post.php',['id'=>1,'name'=>'张三'],$file);

服务端得到文件 处理的代码

  1. $files = $_FILES['media'];
  2. # 得到扩展名
  3. $ext = pathinfo($files['name'],PATHINFO_EXTENSION);
  4. # 上传后的文件名
  5. $name = time().'.'.$ext;
  6. # 上传到服务器的绝对路径
  7. $realpath = __DIR__.'/up/'.$name;
  8. move_uploaded_file($files['tmp_name'],$realpath);

封装工具类(推荐)

  1. function http_request($url,$ret='',$file=''){
  2. if (!empty($file)) { // 有文件上传
  3. # php5.5之前 '@'.$file;就可以进地文件上传
  4. # $ret['pic'] = '@'.$file;
  5. # php5.6之后用此方法
  6. $ret['pic'] = new CURLFile($file);
  7. }
  8. // 初始化
  9. $ch = curl_init();
  10. // 相关设置
  11. # 设置请求的URL地址
  12. curl_setopt($ch,CURLOPT_URL,$url);
  13. # 请求头关闭
  14. curl_setopt($ch,CURLOPT_HEADER,0);
  15. # 请求的得到的结果不直接输出,而是以字符串结果返回 必写
  16. curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
  17. # 设置请求的超时时间 单位秒
  18. curl_setopt($ch,CURLOPT_TIMEOUT,30);
  19. # 设置浏览器型号
  20. curl_setopt($ch,CURLOPT_USERAGENT,'MSIE001');
  21. # 证书不检查
  22. curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,0);
  23. curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,0);
  24. # 设置为post请求
  25. if($ret){ # 如果 $ret不为假则是post提交
  26. # 开启post请求
  27. curl_setopt($ch,CURLOPT_POST,1);
  28. # post请求的数据
  29. curl_setopt($ch,CURLOPT_POSTFIELDS,$ret);
  30. }
  31. // 发起请求
  32. $data = curl_exec($ch);
  33. // 有没有发生异常
  34. if(curl_errno($ch) > 0){
  35. // 把错误发送给客户端
  36. echo curl_error($ch);
  37. $data = '';
  38. }
  39. // 关闭请求
  40. curl_close($ch);
  41. return $data;
  42. }
  43. # GET
  44. #echo http_request('https://wx.1314000.cn/');