我不熟悉HTTP请求技术,我在网上进行了一些搜索,例如我看到有关HTTP请求的w3schools article,但我并不了解我将如何制作我想做的事。 / p>
更具体地说,我想在我的网站页面中有一个按钮向另一个站点(这个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}]}';
?>
我想要做的是每当有人登录我的网站并点击此按钮检查该$ user是否具有来自表格患者的$ spid(例如:SPID9999),然后从其他网站获取数据(http://localhost/SensorPlatform/index.php)并将其存储在我的数据库表中,该表名为sensors。
很抱歉,我没有提供任何代码,但无法找到该怎么做。 谢谢你的时间。
答案 0 :(得分:0)
我可以假设您可以对用户进行验证吗?我现在要。
提出请求的最简单方法是使用file_get_contents()
$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;
如果您需要自定义请求标头,请添加一个数组和一个额外的卷曲选项:
$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请求:
准备帖子数据并添加几个卷曲选项:
$post = 'key1=value1&key2=value2&key3=value3';
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch,CURLOPT_POST,true);