我正在尝试使用PHP创建一个简单的API,但数据不会发布到文件中。
$url = "http://localhost/api.php";
$session = curl_init();
curl_setopt($session, CURLOPT_URL, $url);
curl_setopt($session, CURLOPT_REFERER, "http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']);
curl_setopt($session, CURLOPT_POST, true);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
//curl_setopt($session, CURLOPT_TIMEOUT, 200);
curl_setopt($session, CURLOPT_HEADER, true);
curl_setopt($session, CURLOPT_HTTPHEADER, array('Content-Type: multipart/form-data'));
curl_setopt($session, CURLOPT_POSTFIELDS, http_build_query(array("process"=>"login","user"=>$_POST['user'],"pass"=>$_PO ST['pass'])));
printArr2($session);
$result = curl_exec($session);
$httpCode = curl_getinfo($session, CURLINFO_HTTP_CODE);
curl_close($session);
以下是API
if(isset($_RESPONSE['process']))
{
if(!strcmp($_RESPONSE['content']['process'],"login"))
{
$con = dbConnect();
$str = userLogin($con,$_POST['content']['user'],$_POST['content']['pass']);
if(is_bool($str))
{
$jsonData = json_encode($str);
header("HTTP/1.1 200 OK");
header("Content-type: application/json");
echo $jsonData;
}
else
{
$jsonData = json_encode($str);
header("HTTP/1.1 401 Unauthorised access");
header("Content-type: application/json");
header("Location: ".$_SERVER['HTTP_REFERER']);
echo $jsonData;
}
dbClose($con);
exit;
}
if(!strcmp($_POST['process'],"plagiarism"))
{
$con = dbConnect();
$user = $_POST['user'];
$text = $_POST['text'];
}
}
else
{
$jsonData = json_encode(array("Error"=>"No methods called"));
if(isset($_SERVER['HTTP_REFERER']))
{
header('HTTP\1.1 400', true, 400);
header("Content-type: application/json");
header("Location: ".$_SERVER['HTTP_REFERER']);
echo $jsonData;
exit;
}
else
{
echo "Invalid entry";
}
}
无论我做什么,输出始终是isset($_RESPONSE['process'])
的“其他”部分。
我尝试将“过程”添加到URL中。
$url = "http://localhost/checkapi.php?process=login";
答案 0 :(得分:1)
You are doing a post request, the best way to access those variables is:
vardump($_POST);
Try printing that in your API before the if so you can debug.
答案 1 :(得分:1)
You are using an invalid array index (['content']) in your $_RESPONSE and $_POST super globals.
if(!strcmp($_RESPONSE['content']['process'],"login"))
{
$con = dbConnect();
$str = userLogin($con,$_POST['content']['user'],$_POST['content']['pass']);
Instead you need:
if(!strcmp($_POST['process'],"login"))
{
$con = dbConnect();
$str = userLogin($con,$_POST['user'],$_POST['pass']);