**** Trang thao tác api
$url = 'api'; // link đến trang giaohangf1 để thao tác
$ch = curl_init($url);
$data_string = array(array("method"=>"isLogin"), array("email" => "thanhcong", "password" => "123456"));
$data_string = json_encode($data_string);
$result = file_get_contents($url, null, stream_context_create(array(
'http' => array(
'method' => 'POST',
'header' => 'Content-Type: application/json' . "\r\n"
. 'Content-Length: ' . strlen($data_string) . "\r\n",
'content' => $data_string,
),
)));
$decoded = json_decode($result,true);
echo "<pre>";
print_r($decoded);
if($decoded['resultCode'] == 1){
echo "c or l = ".$decoded['resultMessage']."<br>";
}else{
echo "c or l = ".$decoded['resultMessage']."<br>";
}
$data = json_decode($result,true);
echo "isValid = ".$data['isValid'];
exit;
**** Xử lý api
header('Access-Control-Allow-Origin: *');
header("Content-Type: application/json");
$jsonInput = file_get_contents('php://input');
$decoded = json_decode($jsonInput,true);
$key = $decoded[0]['method'];
$item = $decoded[1];
switch($key) {
case "isLogin"://1
isLogin($item);
break;
}
function isLogin($item){
$email = $item["email"];
$password = $item["password"];
if($email == "thanhcong" && $password == "123456"){
$json = array("resultCode" => 1, "resultMessage" => "c");
}else{
$json = array("resultCode" => 0, "resultMessage" => "l");
}
$json = json_encode($json);
$json = str_replace('\\/', '/', $json);
echo $json;
}
exit;
Không có nhận xét nào:
Đăng nhận xét