は、私の会社は、RESTfulなAPI呼び出しを処理する方法の一例です。
まず、switch文で呼び出しを処理するphpファイルがあります。異なるアクションを前記のファンクションおよびクラスにルーティングする。
/* Class file that is called on this page */
include_once "$_SERVER[DOCUMENT_ROOT]/classes/class.myclass.php";
/**
* This function makes it simpler to stop it from working for debugging purposes.
* All we have to do is comment out the one line of code apiCall($_REQUEST);
* You could have this outside of the function and it would work just as well.
* @param type $REQUEST
*/
function apiCall($REQUEST) {
$con = new MyClass();
switch ($REQUEST['action']) {
case 'getList':
/* Setting the content type to json means that the developer can
* expect a response in the form of parseable json.
*/
header('Content-Type: application/json');
echo json_encode($con->getList($REQUEST));
case 'setValue':
header('Content-Type: application/json');
echo json_encode($con->setValue($REQUEST));
case 'login':
if ($con->login($REQUEST)) {
header('Location: /index.php');
} else {
header('Content-Type: /login.php?status=Failed+Login');
}
default:
header('Content-Type: application/json');
/* If an invalid action was sent in, then this error message will be sent
* back to the user
*/
echo json_encode(['status' => 'Invalid API Call']);
}
}
/* Using $_REQUEST allows developers to access the api via GET or POST */
apiCall($_REQUEST);
次に、私たちが呼び出したさまざまなクラスのすべてのロジックを処理します。使用JSON
class MyClass {
public function getList($REQUEST) {
$id = $REQUEST['id'];
/* code */
return ['status' => 'ok', 'results' => $array];
}
public function setList($REQUEST) {
/* code */
return ['status' => 'ok'];
}
public function login($REQUEST) {
/* code */
$_SESSION['user_id'] = $user_id;
return $login_successful;
}
}
はAJAX
の呼び出しを介して情報を送信するアプリケーションに適しています。 header('Location:')
を使用すると、ajaxのないフォーム提出に適しています。
JavaScriptのajax呼び出しを使用したり、データの送信をどのように処理するかに基づいて送信できます。
jQuery.getJSON
$.getJSON('/switch.php', $.param({id: id, action: 'getList'}), function (json) {
if (json) {
/*code*/
}
});
の使用例次に、あなたは、通常のフォームの送信のためのスイッチのページにしてアクションを持つ隠し入力を渡します。
<form action="/switch.php" method="post">
<!--hidden input named action to direct which switch to use-->
<input name="action" value="login" type="hidden"/>
<input name="username"/>
<input name="password" type="password"/>
<input type="submit"/>
</form>
これらの例は、html/JavaScriptウェブアプリケーション向けです。あなたがJAVA、Python、。NETなどの言語を使用している場合は、REST APIを使用してJSONを解析してアプリケーションロジックの処理方法を把握するだけで簡単です。
file_get_contents
またはcurl
を使用してphpからphiへの呼び出しを実行することもできます。
$data = [
'action' => 'setValue',
'information' => 'More'
];
$json = json_decode(file_get_contents('/switch.php?' . http_build_query($data)),true);
if(!empty($json)){
/*code*/
}
あなたは、コールごとに別々のページを作成し、すべての要求にaction
に渡す心配する必要はありませんでした。しかし、あなたのファイルツリーはこのように見えるようになります。
/api/loginSubmit.php
/api/login.php
/api/getListFromId.php
/api/getList.php
/api/setValues.php
/api/getValues.php
それは、問題がどこにあるかを把握するために、すべてのこれらのファイルを横断するために本当に退屈取得します。
phpに慣れていない場合は、PhalconをAPIを作成するのが好きではなく、単純なAPIをコアPHPで作成してみてください。この投稿があなたに役立つことを願っています... [リンク] http://stackoverflow.com/questions/4973156/how-to-write-a-rest-api [リンク] –
*自分が選んだ言語を使ってAPIを呼び出すことができますか?」これはAPIの背後にあるアイデアの1つです。デカップリング。 *「https」になるため、私が見たり、検討しなければならないことがあれば、私はもう1つ質問します。 [* "。HTTPSは、トランスポート層セキュリティで暗号化された接続内のHTTP(Hypertext Transfer Protocol)による通信で構成されています" *](https://en.wikipedia.org/wiki/OSI_model) HTTPSは道のりです。 – jDo
URLを投稿した隠れたパラメータを追加して、データを投稿する最終メソッドを追加することができます。 – KDOT