我们的客户端给了我一个REST API,我需要对它进行PHP调用。但事实上,API提供的文档非常有限,所以我真的不知道如何调用该服务。

我试着谷歌它,但唯一出现的是一个已经过期的雅虎!关于如何调用服务的教程。不要提及标题或任何深入的信息。

是否有关于如何调用REST API的适当信息或相关文档?因为即使在w3学校中,它们也只描述SOAP方法。在PHP中创建API的其他选项有哪些?


当前回答

CURL是最简单的方法。这里有一个简单的调用

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "THE URL TO THE SERVICE");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, POST DATA);
$result = curl_exec($ch);


print_r($result);
curl_close($ch);

其他回答

你可以通过php的cURL扩展访问任何REST API。但是,API文档(方法,参数等)必须由您的客户端提供!

例子:

// Method: POST, PUT, GET etc
// Data: array("param" => "value") ==> index.php?param=value

function CallAPI($method, $url, $data = false)
{
    $curl = curl_init();

    switch ($method)
    {
        case "POST":
            curl_setopt($curl, CURLOPT_POST, 1);

            if ($data)
                curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
            break;
        case "PUT":
            curl_setopt($curl, CURLOPT_PUT, 1);
            break;
        default:
            if ($data)
                $url = sprintf("%s?%s", $url, http_build_query($data));
    }

    // Optional Authentication:
    curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    curl_setopt($curl, CURLOPT_USERPWD, "username:password");

    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

    $result = curl_exec($curl);

    curl_close($curl);

    return $result;
}

如果你有一个url并且你的php支持它,你可以调用file_get_contents:

$response = file_get_contents('http://example.com/path/to/api/call?param1=5');

如果$response是JSON,使用json_decode将其转换为php数组:

$response = json_decode($response);

如果$response是XML,使用simple_xml类:

$response = new SimpleXMLElement($response);

http://sg2.php.net/manual/en/simplexml.examples-basic.php

实际上有很多客户。其中之一是佩斯,看看这个。请记住,这些REST调用是简单的http请求,具有各种方法:GET、POST、PUT和DELETE。

正如@Christoph Winkler提到的,这是实现它的基类:

curl_helper.php

// This class has all the necessary code for making API calls thru curl library

class CurlHelper {

// This method will perform an action/method thru HTTP/API calls
// Parameter description:
// Method= POST, PUT, GET etc
// Data= array("param" => "value") ==> index.php?param=value
public static function perform_http_request($method, $url, $data = false)
{
    $curl = curl_init();

    switch ($method)
    {
        case "POST":
            curl_setopt($curl, CURLOPT_POST, 1);

            if ($data)
                curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
            break;
        case "PUT":
            curl_setopt($curl, CURLOPT_PUT, 1);
            break;
        default:
            if ($data)
                $url = sprintf("%s?%s", $url, http_build_query($data));
    }

    // Optional Authentication:
    //curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    //curl_setopt($curl, CURLOPT_USERPWD, "username:password");

    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

    $result = curl_exec($curl);

    curl_close($curl);

    return $result;
}

}

然后你就可以包含这个文件并使用它,例如:any.php

    require_once("curl_helper.php");
    ...
    $action = "GET";
    $url = "api.server.com/model"
    echo "Trying to reach ...";
    echo $url;
    $parameters = array("param" => "value");
    $result = CurlHelper::perform_http_request($action, $url, $parameters);
    echo print_r($result)

您可以使用file_get_contents来发出任何http POST/PUT/DELETE/OPTIONS/HEAD方法,除了函数名所示的GET方法之外。

如何在PHP中使用file_get_contents发布数据?