2016-12-02 81 views
0

我对PHP相当陌生,但我有一个“API”设置在我的Web服务器上以发送/接收数据到MySQL数据库。

由于某种原因,当我向我的register.php文件(注册入口点)发送http post请求时,$_POST变量保持为空。但是,在使用phpconfig()并做了一些挖掘之后,我注意到我发送的参数存储在$_REQUEST变量中,所以我相应地更改了我的register.php并且它工作正常。

我的问题是为什么发生这种情况,以及是否使用$_REQUEST是错误的。

我用angularjs' $ HTTP发送HTTP请求:

var link = 'http://www.temporaryurl.com/register.php' 

$http.post(link, {'name': 'Adam'},{'email': '[email protected]'},{'password': 'testingPass!'}).then(function(response) { 
console.log("Http success!"); 
console.log(response); 
}, function(response) { 
console.log("Http failure"); 
console.log(response); 
}); 

这里的register.php

<?php 
require_once 'include/DB_Functions.php'; 
$db = new DB_Functions(); 

// json response array 
$response = array("error" => FALSE); 

if (isset($_REQUEST['name']) && isset($_REQUEST['email']) && isset($_REQUEST['password'])) { 

    // receiving the post params 
    $name = $_REQUEST['name']; 
    $email = $_REQUEST['email']; 
    $password = $_REQUEST['password']; 

    // Remove all illegal characters from email 
    $email = filter_var($email, FILTER_SANITIZE_EMAIL); 

    // Validate e-mail 
    if (!filter_var($email, FILTER_VALIDATE_EMAIL) === false) { 
     echo("$email is a valid email address"); 
    } else { 
     echo("$email is not a valid email address"); 
    } 

    if (!filter_var($email, FILTER_VALIDATE_EMAIL) === false) { 
     // check if user is already existed with the same email 
     if ($db->isUserExisted($email)) { 
      // user already existed 
      $response["error"] = TRUE; 
      $response["error_msg"] = "User already existed with " . $email; 
      echo json_encode($response); 
     } else { 
      // create a new user 
      $user = $db->storeUser($name, $email, $password); 
      if ($user) { 
       // user stored successfully 
       $response["error"] = FALSE; 
       $response["uid"] = $user["unique_id"]; 
       $response["user"]["name"] = $user["name"]; 
       $response["user"]["email"] = $user["email"]; 
       $response["user"]["created_at"] = $user["created_at"]; 
       $response["user"]["updated_at"] = $user["updated_at"]; 
       echo json_encode($response); 
      } else { 
       // user failed to store 
       $response["error"] = TRUE; 
       $response["error_msg"] = "Unknown error occurred in registration!"; 
       echo json_encode($response); 
      } 
     } 
    } else { 
     echo("$email is not a valid email address"); 
     $response["error"] = TRUE; 
     $response["error_msg"] = "Email is invalid!"; 
     echo json_encode($response); 
    } 
} else { 
    $response["error"] = TRUE; 
    $response["error_msg"] = "Required parameters (name, email or password) is missing!"; 
    echo json_encode($response); 
} 
?> 
+0

有一刻,我会更新我的帖子。 – awbasham

+1

参考这篇文章http://stackoverflow.com/questions/15485354/angular-http-post-to-php-and-undefined并尝试。 – Perumal

+0

'if(x === false)'是一个需要消除的反模式,只要使用'if(!x)'和'if(!x === false)'更加荒谬。这读作为三重否定:“如果不是字面上不假”。尽量避免像这样毫无意义的杂技。同样,我敢打赌,你的电子邮件“验证”表达式会拒绝许多绝对有效的地址,因为像'x @ google'这样的技术在技术上是有效的,尽管看起来可能有多荒谬。 – tadman

回答

0

我的问题是双重的。一,我认为$ _POST是空的,因为我天真地测试register.php文件,通过在我的web浏览器中输入url(我忘记使用chrome扩展来强制POST而不是GET)。

而且,我在angularjs文件名为$ HTTP作为本:

$http({ 
    method: 'POST', 
    url: link, 
    data: data1, 
    headers: { 
     'Content-Type': 'application/x-www-form-urlencoded' 
    }}).then(function(response) { 
     console.log("Http success!"); 
     console.log(response); 
    }, function(response) { 
     console.log("Http failure!"); 
     console.log(response); 
    }); 

我想帮助被定义的Content-Type为 '应用程序/ X WWW的形式,进行了urlencoded',并通过数据1作为

var data1 = "name=adam&[email protected]&password=testPass!";