【问题标题】:Calling a php function through javascript [duplicate]通过javascript调用php函数[重复]
【发布时间】:2013-06-03 19:18:42
【问题描述】:

我想使用 webiopi RESTful api,使用 ajax 显示和更改 gpio 变量。

我有一个可以使用的 php 函数,但我无法放弃使用 javascript(ajax?)调用它,或者这不是最佳实践。

api的一个例子。

设置GPIO功能
HTTP POST /GPIO/(gpioNumber)/function/("in" or "out" or "pwm")
返回新设置:“in”或“out”或“pwm”
示例:
将 GPIO 0 设置为输入:HTTP POST /GPIO/0/function/in
将 GPIO 1 设置为输出:HTTP POST /GPIO/1/function/out

获取 GPIO 值
HTTP GET /GPIO/(gpioNumber)/value
返回 0 或 1
示例:
获取 GPIO 0 值:HTTP GET /GPIO/0/value

还有php函数

function WebIOPi($path, $method) {
    $url = 'http://192.168.1.170'.$path;

    // Open Connection
    $ch = curl_init();

    // set the url, user/pass, port
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_USERPWD, 'webiopi:raspberry');
    curl_setopt($ch, CURLOPT_PORT, 8000);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

    // Execute POST
    $result = curl_exec($ch);

    // Close Connection
    curl_close($ch);
    return $result;
}

我假设我也可以以某种方式创建一个数据数组,并创建一个带有按钮的表单来控制引脚。而不是为每个按钮等都这样做。

编辑*

这是我尝试使用建议的方法,仍然无法与其他服务器通信,我哪里出错了。

    <?php //goes here
    function WebIOPi($path, $method) {
        $url = 'http://192.168.0.17'.$path;

    // Open Connection
    $ch = curl_init();

    // set the url, user/pass, port
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_USERPWD, 'webiopi:pass');
    curl_setopt($ch, CURLOPT_PORT, 8000);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

    // Execute POST
    $result = curl_exec($ch);

    // Close Connection
    curl_close($ch);
    return $result;
}
?>

<!DOCTYPE HTML>
<html>
<head>
    <meta charset="utf-8">
    <title>Page Title</title>
    <link rel="stylesheet" href="css/style.css" />
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
</head>
<body>
<script>
$.post('http://192.168.0.17/api.php', 
    { 'method':'POST', 'path':'/GPIO/4/function/out' }, 
    function(data) {
        alert(data);
});
</script>
</body>
</html>

【问题讨论】:

  • 在大多数情况下,仅在您自己的服务器上调用处理跨域请求的 PHP 脚本比从客户端执行要容易得多。只需将该函数粘贴到 PHP 文件中,调用它并回显结果,这样就可以使用 ajax 调用调用脚本并返回 cURL 形式的内容。
  • 它现在服务器到服务器都在同一个域(本地主机)上运行。一台服务器使用:80 另一台服务器:8000。另外我是新来的休息,并没有真正接触太多 JavaScript,所以我不确定如何。

标签: php javascript ajax curl


【解决方案1】:

api.php中创建一个php文件:

$path = $_POST['path'];
$method = $_POST['method'];
... whatever validation you want to do ...
return WebIOPi($path, $method);

然后,在您的网站中包含 jQuery(这不是必需的,但会使 Ajax 更容易):

//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js

然后像这样创建一个 JavaScript 函数,它将发送一个带有指定数据的请求,然后给你一个带有响应的警报:

$.post('http://whatever/api.php', 
    { 'method':'someMethod', 'path':'somePath' }, 
    function(data) {
        alert(data);
});

【讨论】:

  • 我想我现在明白了,所以我可以说,{ 'method':'GET', 'path':'GPIO/1/value' }, ?或者它对 get 不起作用,或者我什至没有正确设置变量?
  • @OverRipeBananas 没有。如果要使用GET,则切换到$.get功能。
  • 并且仍然将方法设置为GET,它被发送到php函数中的$method var?
  • 如果你使用 jQuery 的$.get 表示法(docs),它会自动假设你想使用GET 方法。我的示例是 POST 方法,但它们非常相似。
猜你喜欢
  • 2015-06-01
  • 2012-09-30
  • 1970-01-01
  • 2012-09-12
  • 1970-01-01
  • 2012-12-30
  • 1970-01-01
  • 2014-02-01
  • 2012-11-29
相关资源
最近更新 更多