我正在尝试使用 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”部分。
我尝试将“process”作为 get 附加到 URL 中。
$url = "http://localhost/checkapi.php?process=login";
最佳答案
您在 $_RESPONSE 和 $_POST super 全局变量中使用了无效的数组索引(['content'])。
if(!strcmp($_RESPONSE['content']['process'],"login"))
{
$con = dbConnect();
$str = userLogin($con,$_POST['content']['user'],$_POST['content']['pass']);
相反,您需要:
if(!strcmp($_POST['process'],"login"))
{
$con = dbConnect();
$str = userLogin($con,$_POST['user'],$_POST['pass']);
关于php - 使用 cURL 时数据未发布到 php 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36405813/