【发布时间】:2016-04-04 14:43:28
【问题描述】:
我正在尝试使用 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']) 的“else”部分。
我尝试通过将“进程”附加到 URL 来将其作为获取。
$url = "http://localhost/checkapi.php?process=login";
【问题讨论】:
-
没有发布到什么文件?您没有将其保存到任何文件中。
-
我已经粘贴了 api。看看
-
我认为您将 $_REQUEST 与 $_RESPONSE 混淆了。但是,正如 daniel axel 在下面所写,请改用 $_POST。
-
{"Host":"localhost","Accept":"\/","Referer":"http:\/\/localhost\/checkapi.php ","Content-Type":"plain\/text","Content-Length":"43"}
-
谢谢你们的工作。还有一件事我想请你帮忙。