使用cURL时,数据不会被发布到php文件中


Data is not getting posted to php file while using cURL

我试图使用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'])的"其他"部分。

我尝试将"process"作为get添加到URL中。

$url = "http://localhost/checkapi.php?process=login";

您在$_RESPONSE和$_POST超级全局变量中使用了无效的数组索引(['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']);

您正在进行post请求,访问这些变量的最佳方式是:

vardump($_POST);

尝试在if之前将其打印到API中,以便进行调试。