php - 按钮发出 HTTP 请求并将数据存储在数据库中

标签 php html mysql asp.net httprequest

我对HTTP请求技术不熟悉,我在网上做了一些搜索,例如我看到w3schools article关于 HTTP 请求,但我不明白如何做出我想做的事情。

更具体地说,我希望我的网站页面中有一个按钮可以向另一个网站(此 http://localhost/SensorPlatform/index.php )发出 http 请求,该网站在 index.php 中包含此代码/数据。

<?php
header('Content-type: application/json');

echo'{"ID":"SPID9999","RSSI":-48,"Time":"","sensors":[{"Type":"AirFlow","Unit":"Analog","Val":0},{"Type":"Temperature","Unit":"C","Val":28.65},{"Type":"SkinConduct","Unit":"microSiemens","Val":-1.00},{"Type":"SkinResist","Unit":"Ohm","Val":-1.00},{"Type":"SkinConductVolt","Unit":"V","Val":0.49},{"Type":"HeartRate","Unit":"BPM","Val":0},{"Type":"02 Saturation","Unit":"%","Val":0},{"Type":"ElectroCardioGram","Unit":"Analog","Val":3.78},{"Type":"BodyPosition","Unit":"^<>_|","Value":3}]}';

?>

我想要做的是,每当有人登录到我的网站并单击此按钮时,检查此 $username 是否具有来自表 Patient 的 $spid(例如:SPID9999),然后从其他网站获取数据(http://localhost/SensorPlatform/index.php)并将其存储在我的数据库表中,该表名为传感器。

抱歉,我没有提供任何代码,但找不到该怎么做。 感谢您抽出时间。

最佳答案

我可以假设您可以验证用户吗?我现在会的。

发出请求的最简单方法是使用 file_get_contents()

进行 GET
$json= file_get_contents('http://localhost/SensorPlatform/index.php');
$data = json_encode($json,true);

json_encode($json,true) 为您提供 JSON 数据的数组。

另一种是使用curl,它的用途更加广泛,可以执行HTTP POST 请求。一旦克服了学习曲线,学习曲线就会变得简单可靠。

这相当于 file_get_contents();

  $ch = curl_init('http://localhost/SensorPlatform/index.php');
  curl_setopt($ch, CURLOPT_ENCODING,"");
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_FILETIME, true);
  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 100);
  curl_setopt($ch, CURLOPT_VERBOSE, true);
  curl_setopt($ch, CURLOPT_TIMEOUT,100);
  curl_setopt($ch, CURLOPT_FAILONERROR,true);
  $json = curl_exec($ch);
  $data = json_encode($json,true);

此处的curl设置用于测试HTTP GET请求。

  $ch = curl_init('http://localhost/SensorPlatform/index.php');
  curl_setopt($ch, CURLOPT_ENCODING,"");
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_HEADER, true);
  curl_setopt($ch, CURLINFO_HEADER_OUT, true);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  curl_setopt($ch, CURLOPT_FILETIME, true);
  curl_setopt($ch, CURLOPT_USERAGENT,"Mozilla/5.0 (Windows NT 5.1; rv:32.0) Gecko/20100101 Firefox/32.0");
  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 100);
  curl_setopt($ch, CURLOPT_VERBOSE, true);
  curl_setopt($ch, CURLOPT_AUTOREFERER, true);
  curl_setopt($ch, CURLOPT_TIMEOUT,100);
  curl_setopt($ch, CURLOPT_FAILONERROR,true);

  $data = curl_exec($ch);
  if (curl_errno($ch)){
      $data .= 'Retreive Base Page Error: ' . curl_error($ch);
  }
  else {
    $skip = intval(curl_getinfo($ch, CURLINFO_HEADER_SIZE)); 
    $responseHeader = substr($data,0,$skip);
    $data= substr($data,$skip);
    $info = curl_getinfo($ch);
    $info = var_export($info,true);
   }
  echo $responseHeader;
  echo $info;
  echo $data;

如果您需要自定义请求 header ,请添加一个数组和一个附加的curl选项:

    $request = array();
    $request[] = "Host: www.example.com";
    $request[] = "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
    $request[] = "User-Agent: MOT-V9mm/00.62 UP.Browser/6.2.3.4.c.1.123 (GUI) MMP/2.0";
    $request[] = "Accept-Language: en-US,en;q=0.5";
    $request[] = "Connection: keep-alive";
    $request[] = "Cache-Control: no-cache";
    $request[] = "Pragma: no-cache";


curl_setopt($ch, CURLOPT_HTTPHEADER, $request);

如果您需要执行 POST 请求:

准备发布数据并添加几个curl选项:

$post = 'key1=value1&key2=value2&key3=value3';

curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch,CURLOPT_POST,true);

关于php - 按钮发出 HTTP 请求并将数据存储在数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29528248/

相关文章:

PHP 通过 exec 运行 linux "less"命令 - 二进制文件警告

java - JSONException : Value of type java. lang.String解析时无法转换为JSONObject

javascript - 如何读取视频标签中的所有视频类型

javascript - 在 <p> 中获取下拉框更改值(来自 MySQL)

mysql:转身就好

mysql - 使用 where 子句替换/插入重复键 [mySQL]

php - 如何在 Zend-Framework 中创建或执行 MySQL 过程?

javascript - jquery超时改变div

jquery - 文字过渡效果

用于获取 Joomla 用户及其配置文件字段的 MySQL 查询