【问题标题】:Button makes an HTTP Request and Store the data in the database按钮发出 HTTP 请求并将数据存储在数据库中
【发布时间】:2015-04-09 01:34:08
【问题描述】:

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

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

<?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 是否具有来自 table Patient 的此 $spid(例如:SPID9999),然后从其他站点获取数据(http://localhost/SensorPlatform/index.php) 并将其存储在我的名为传感器的数据库表中。

抱歉,我没有提供任何代码,但找不到要做什么。 感谢您的宝贵时间。

【问题讨论】:

  • 您想在单击表单按钮并将其提交给 PHP 脚本时发出此请求?请求将由 PHP 发出?
  • @Misunderstood 我不介意按钮是否在表单标签中。我想要的是单击此按钮,进行必要的 if 语句来检查用户名和 spid,然后将此数据存储在我的数据库中

标签: php html mysql asp.net httprequest


【解决方案1】:

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

发出请求的最简单方法是使用 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() 的 curl 等效项;

  $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;

如果您需要自定义请求标头,请添加一个数组和一个额外的 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);

【讨论】:

  • 感谢您的宝贵时间。在第一行中,您是否忘记了链接的' ',因为我收到了错误消息。我也收到此错误Parse error: syntax error, unexpected 'curl_setopt' (T_STRING) in E:\xampp\htdocs\ptixiaki\ptixiaki.php on line 7
  • 知道了,在我发布之前我看不到错误,然后我再编辑一些。
  • 我忘了写 file_get_contents() 这就是我收到错误的原因。现在我得到了这个 Parse error: syntax error, unexpected '$json' (T_VARIABLE) in E:\xampp\htdocs\ptixiaki\ptixiaki.php on line 3 。所有这些代码我都放在一个文件中对吗?
  • 您的请求可能失败。可能没有 JSON。我需要查看代码。请发布您当前的代码。
  • 产生错误的代码。 ptixiaki.php
猜你喜欢
  • 2018-07-07
  • 1970-01-01
  • 2021-12-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-06
  • 2011-06-24
相关资源
最近更新 更多