通过javascript调用php函数[重复]

This question already has an answer here:

I'm wanting to use webiopi RESTful api, to display and change the gpio variables using ajax.

I have a php function I can use but i can't quit get my head around calling it using javascript (ajax?) or is this not best practice.

The an example of the api.

Set GPIO function
HTTP POST /GPIO/(gpioNumber)/function/("in" or "out" or "pwm")
Returns new setup : "in" or "out" or "pwm"
Examples:
To set GPIO 0 as input : HTTP POST /GPIO/0/function/in
To set GPIO 1 as output : HTTP POST /GPIO/1/function/out

Get GPIO value
HTTP GET /GPIO/(gpioNumber)/value
Returns 0 or 1
Example :
To get GPIO 0 value : HTTP GET /GPIO/0/value

And the php function

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

I'm assuming i could also somehow create an array of data, and create a form with buttons to control the pins. Rather than do it for each button etc..

Edit*

This is what I've tried using the suggestions, still cannot communicate with rest server, where am i going wrong.

    <?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>
</div>

Make a php file with this in, api.php:

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

Then, include jQuery in your website (this isn't NEEDED but makes Ajax much easier):

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

Then make a JavaScript function like this, which will send a request with the data specified, then give you an alert with the response:

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