苗条的宁静网络服务

时间:2015-09-28 07:52:40

标签: cors httpurlconnection slim postman

我在slim中有一个简单的restful webservice,托管在byxost上。

require 'vendor/autoload.php';

require 'database.php';

use Slim\Slim;


//for cors
$corsOptions = array(
    "origin" => "*",
    "exposeHeaders" => array("Content-Type", "X-Requested-With", "X-authentication", "X-client"),
    "allowMethods" => array('GET', 'POST', 'PUT', 'DELETE', 'OPTIONS')
);

$app = new Slim();

$app->add(new \CorsSlim\CorsSlim($corsOptions));


//2 params
//first - device_id
//second - state
$app->get('/api/changestate','changeState');

//1 param
//param = value
$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');




function changeState(){
    global $pdo;

    $app = Slim::getInstance();

    $deviceId = $app->request->params('device_id');

    //1-ON
    //0-OFF
    $state = $app->request->params('state');

    $sql  = 'UPDATE device SET state=? WHERE id=?';

    $perepareStatement  = $pdo->prepare($sql);

    $perepareStatement->execute(array($state,$deviceId));

}

function changeTemp(){
    global $pdo;

    $app = Slim::getInstance();

    $value = $app->request->params('value');


    $sql = 'UPDATE sensor SET value=? WHERE id = 1';

    $perepareStatement = $pdo->prepare($sql);

    $perepareStatement->execute(array($value));

}

function openAll(){
    global $pdo;

    $sql = 'UPDATE device SET state=1';

    $perepareStatement = $pdo->prepare($sql);

    $perepareStatement->execute(array());
}

function closeAll(){
    global $pdo;

    $sql = 'UPDATE device SET state=0';

    $perepareStatement = $pdo->prepare($sql);

    $perepareStatement->execute(array());
}


function getState(){

    global $pdo;

    $sql = 'SELECT * FROM device';

    $query = $pdo->query($sql);

    $result = $query->fetchAll(PDO::FETCH_ASSOC);

    echo json_encode($result);

}


function getTemp(){

    global $pdo;

    $sql = 'SELECT * FROM sensor';

    $query = $pdo->query($sql);

    $result = $query->fetchAll(PDO::FETCH_ASSOC);

    echo json_encode($result);


}

$app->run();



?>

当我从浏览器访问此Web服务时,它运行正常。但是当我尝试使用Httpurlconnection类从Postman rest客户端或android中访问时,它给出了以下响应:

<html>
    <body>
        <script type="text/javascript" src="/aes.js" ></script>
        <script>function toNumbers(d){var e=[];d.replace(/(..)/g,function(d){e.push(parseInt(d,16))});return e}function toHex(){for(var d=[],d=1==arguments.length&&arguments[0].constructor==Array?arguments[0]:arguments,e="",f=0;f
            <d.length;f++)e+=(16>d[f]?"0":"")+d[f].toString(16);return e.toLowerCase()}var a=toNumbers("f655ba9d09a112d4968c63579db590b4"),b=toNumbers("98344c2eee86c3994890592585b49f80"),c=toNumbers("4a187380bc9ff2b039ef2a8c5d6a5a12");document.cookie="__test="+toHex(slowAES.decrypt(c,2,a,b))+"; expires=Thu, 31-Dec-37 23:55:55 GMT; path=/";location.href="http://homecontrol.byethost32.com/api/gettemp?ckattempt=1";
            </script>
            <noscript>This site requires Javascript to work, please enable Javascript in your browser or use a browser with Javascript support</noscript>
        </body>
    </html>

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

1 个答案:

答案 0 :(得分:1)

看来你的问题与网络有关。从浏览器访问时,您可能正在使用localhost或者您已设置主机地址(例如使用/etc/hosts文件时)。但是,当您从Android设备拨打相同的网址时,您就会从网络外部访问IP /地址。

我想这是因为您所获得的回应似乎与家庭自动化系统有关。温度传感器更精确:http://homecontrol.byethost32.com/api/gettemp?ckattempt=1