Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Cors slimrestfulwebservice_Cors_Httpurlconnection_Slim_Postman - Fatal编程技术网

Cors slimrestfulwebservice

Cors slimrestfulwebservice,cors,httpurlconnection,slim,postman,Cors,Httpurlconnection,Slim,Postman,我在slim中有一个简单的restful Web服务,它由Ethost托管 需要“vendor/autoload.php”; 需要“database.php”; 使用Slim\Slim; //对于cors $corsOptions=数组( “原点”=>“*”, “exposeHeaders”=>数组(“内容类型”、“X-Requested-With”、“X-authentication”、“X-client”), “allowMethods”=>数组('GET','POST','PUT','DE

我在slim中有一个简单的restful Web服务,它由Ethost托管

需要“vendor/autoload.php”;
需要“database.php”;
使用Slim\Slim;
//对于cors
$corsOptions=数组(
“原点”=>“*”,
“exposeHeaders”=>数组(“内容类型”、“X-Requested-With”、“X-authentication”、“X-client”),
“allowMethods”=>数组('GET','POST','PUT','DELETE','OPTIONS')
);
$app=new Slim();
$app->add(新建\CorsSlim\CorsSlim($corsOptions));
//2参数
//第一个-设备id
//第二状态
$app->get('/api/changestate','changestate');
//1参数
//参数=值
$app->get('/api/changetemp','changetemp');
$app->get('/api/openall','openall');
$app->get('/api/closeall','closeall');
$app->get('/api/getstate','getstate');
$app->get('/api/gettemp','gettemp');
函数changeState(){
全球$pdo;
$app=Slim::getInstance();
$deviceId=$app->request->params('device_id');
//一对一
//零分
$state=$app->request->params('state');
$sql='更新设备集状态=?其中id=?';
$perepareStatement=$pdo->prepare($sql);
$perepareStatement->execute(数组($state,$deviceId));
}
函数changeTemp(){
全球$pdo;
$app=Slim::getInstance();
$value=$app->request->params('value');
$sql='更新传感器设置值=?其中id=1';
$perepareStatement=$pdo->prepare($sql);
$perepareStatement->execute(数组($value));
}
函数openAll(){
全球$pdo;
$sql='更新设备集状态=1';
$perepareStatement=$pdo->prepare($sql);
$perepareStatement->execute(array());
}
函数closeAll(){
全球$pdo;
$sql='更新设备集状态=0';
$perepareStatement=$pdo->prepare($sql);
$perepareStatement->execute(array());
}
函数getState(){
全球$pdo;
$sql='SELECT*FROM device';
$query=$pdo->query($sql);
$result=$query->fetchAll(PDO::FETCH_ASSOC);
echo json_编码($result);
}
函数getTemp(){
全球$pdo;
$sql='SELECT*FROM sensor';
$query=$pdo->query($sql);
$result=$query->fetchAll(PDO::FETCH_ASSOC);
echo json_编码($result);
}
$app->run();
?>
当我从浏览器访问此Web服务时,它工作正常。但是,当我尝试使用
Httpurlconnection
class从Postman rest客户端或android中访问时,它会给出以下响应:


函数toNumbers(d){var e=[];d.replace(/(..)/g,函数(d){e.push(parseInt(d,16))});将e}函数返回到hex(){for(var d=[],d=1==参数。长度和参数[0]。构造函数==数组?参数[0]:参数,e=“”,f=0;f
d[f]?“0:”)+d[f].toString(16);返回e.toLowerCase()}var a=toNumbers(“f655ba9d09a112d4968c63579db590b4”)、b=toNumbers(“98344C2EE86C3994890592585B49F80”)、c=toNumbers(“4A187380BC9FF2B039EF2A8C5D6A12”);document.cookie=“\uuu test=“+toHex(slowAES.decrypt(c,2,a,b))+”;expires=Thu,37年12月31日23:55:55 GMT;path=/”;location.href=”http://homecontrol.byethost32.com/api/gettemp?ckattempt=1";
此网站需要Javascript才能工作,请在浏览器中启用Javascript或使用支持Javascript的浏览器

我认为问题与CORS有关,我添加了
corsSlim
,但没有运气。我对这些技术相当陌生。任何帮助都将不胜感激。提前感谢。

您的问题似乎与网络有关。从浏览器访问时,可能您正在使用
localhost
,或者您已经设置了主机地址(如使用
/etc/hosts
文件时)。但是,当您从Android设备调用相同的URL时,您是从网络外部访问IP/地址


我想这是因为你得到的反应似乎与家庭自动化系统有关。更准确地说,是一个温度传感器:。

看起来您必须使用Postman/Android模拟javascript行为。我希望我的rest api能够通过客户端(Android/Postman)连接,而不用担心这个“javascript问题”。比如连接到公共api,比如简单的twitter api。我需要如何使用rest api才能获得这种行为。