博客列表 >PHP POST提交,CURL网络请求

PHP POST提交,CURL网络请求

可乐
可乐原创
2021年10月23日 15:09:33706浏览

POST 提交

  1. <?php
  2. print_r($_REQUEST);
  3. if(!empty($_POST)){
  4. if($_POST['opt'] == '+')
  5. {
  6. $num = (int)$_POST['x'] + (int)$_POST['y'];
  7. }else if($_POST['opt'] == '-')
  8. {
  9. $num = (int)$_POST['x'] - (int)$_POST['y'];
  10. }else if($_POST['opt'] == '*')
  11. {
  12. $num = (int)$_POST['x'] * (int)$_POST['y'];
  13. }else if($_POST['opt'] == '/')
  14. {
  15. $num = (int)$_POST['x'] / (int)$_POST['y'];
  16. }else if($_POST['opt'] == '%')
  17. {
  18. $num = (int)$_POST['x'] % (int)$_POST['y'];
  19. }
  20. echo 'x'. $_POST['opt'] . 'y 计算结果:' .$num;
  21. }else if($_POST['opt'] == '%')
  22. {
  23. $num = (int)$_POST['x'] % (int)$_POST['y'];
  24. }
  25. ?>
  26. <!DOCTYPE html>
  27. <html lang="en">
  28. <head>
  29. <meta charset="UTF-8">
  30. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  31. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  32. <title>Document</title>
  33. </head>
  34. <body>
  35. <h1>计算器</h1>
  36. <form action="" method="post">
  37. <input type="number" name="x" value="<?=isset($_POST['x'])?$_POST['x']:''; ?>" />
  38. <select name="opt">
  39. <option value="+" <?=isset($_POST['opt']) && $_POST['opt']=='+'?'selected':'' ?>>+</option>
  40. <option value="-" <?=isset($_POST['opt']) && $_POST['opt']=='-'?'selected':'' ?>>-</option>
  41. <option value="*" <?=isset($_POST['opt']) && $_POST['opt']=='*'?'selected':'' ?>>*</option>
  42. <option value="/" <?=isset($_POST['opt']) && $_POST['opt']=='/'?'selected':'' ?>>/</option>
  43. <option value="%" <?=isset($_POST['opt']) && $_POST['opt']=='%'?'selected':'' ?>>%</option>
  44. </select>
  45. <input type="number" name="y" value="<?=isset($_POST['y'])?$_POST['y']:''; ?>" />
  46. <input type="submit" value="计算" />
  47. </form>
  48. </body>
  49. </html>

CURL 网络请求

  1. <?php
  2. function get_url($url,$data,$is_post=0){
  3. $ch = curl_init();
  4. curl_setopt($ch, CURLOPT_URL ,'http://apis.juhe.cn/simpleWeather/query?key=abc4b64ae7656b460723402175a5650b&city=合肥');
  5. if($is_post == 0){
  6. if(!empty($data)){
  7. $url .= '?';
  8. foreach($data as $k=>$v){
  9. $url .= $k . '=' . $v . '&';
  10. }
  11. }
  12. }
  13. curl_setopt($ch, CURLOPT_URL ,$url);
  14. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);
  15. curl_setopt($ch, CURLOPT_TIMEOUT, 30);
  16. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  17. curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
  18. curl_setopt($ch, CURLOPT_HEADER, 0);
  19. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  20. curl_setopt($ch, CURLOPT_USERAGENT, 'Data');
  21. curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
  22. if($is_post == 1){
  23. curl_setopt($ch, CURLOPT_POST, 1);
  24. curl_setopt($ch, CURLOPT_POSTFIELDS , $data);
  25. }
  26. $html = curl_exec($ch);
  27. if(curl_errno($ch)){
  28. return curl_errno($ch);
  29. }
  30. curl_close($ch);
  31. return $html;
  32. }
  33. $data = [
  34. 'key' => 'abc4b64ae7656b460723402175a5650b',
  35. 'city' => '合肥'
  36. ];
  37. echo get_url('http://apis.juhe.cn/simpleWeather/query',$data);
  38. ?>

声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议