简体   繁体   English

cURL没有在PHP中发送POST数据

[英]cURL not sending POST data in PHP

I'm trying to make a small API file and for whatever reason the api.php file I'm sending cURL data to via POST is not receiving the data. 我正在尝试制作一个小的API文件,无论出于何种原因,我通过POST发送cURL数据的api.php文件都未接收到该数据。 I've looked endlessly on here and Google and can't seem to figure out what's wrong. 我一直在这里和Google进行无休止的搜索,似乎无法弄清楚出了什么问题。 I've tried various different methods and nothings working. 我尝试了各种不同的方法,但没有任何效果。

Here's my code. 这是我的代码。

client.php: client.php:

$url = 'http://localhost/listcross2/api.php';

  //var_dump($_POST);
  $rCURL = curl_init();

  $params = array('action' => 'login', 
      'username' => $_POST["username"],
       'password' => $_POST["password"]);

  curl_setopt_array($rCURL, array(
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_URL => $url,
    CURLOPT_POST => true,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_REFERER => "http://www.localhost/listcross2/client.php",
    CURLOPT_POSTFIELDS => http_build_query($params)
    )
  );

  $aData = curl_exec($rCURL);

  if(curl_errno($rCURL))
  {
      echo 'error:' . curl_error($rCURL);
  }

  curl_close($rCURL);

  $login_result = json_decode ( $aData, true );

  var_dump($login_result);

api.php api.php

$value = "An error has occurred within API: ";
var_dump($_POST);

// For POST actions
if (!empty($_POST))
{
  switch($_POST["action"])
  {
    case "login":
        if (isset($_POST["username"]) && isset($_POST["password"]))
          $value = login($_POST["username"], $_POST["password"]);
        else
          $value = "Missing argument";
        break;
  }
}

exit(json_encode($value));

When I submit to the API page, I get the following error: 当我提交到API页面时,出现以下错误:

Fatal error: Call to undefined function login() in /var/www/playground/api.php on line 13

If you define a function called login(), you will no longer get this error. 如果定义一个名为login()的函数,则将不再收到此错误。 Also, this code: 另外,此代码:

exit(json_encode($value));

results in a null value because you are passing it an invalid argument. 导致一个空值,因为您要传递一个无效的参数。 If you pass json_encode() an array you will no longer get the null value returned from this function. 如果传递json_encode()一个数组,您将不再获得此函数返回的空值。 A simple example of the login function: 登录功能的简单示例:

function login($username, $password) {
    return array("username"=>$username, "password"=>$password);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM