php - 向 php $_POST 发送 cURL 终端命令返回 null

标签 php mysql debugging post curl

所以我对编写 php 比较陌生,上周开始学习它。自从我开始写它以来,这一直给我带来问题。 SQL 查询可以很好地填充常量变量,但我的应用程序正在尝试根据发布到网络服务器的 lat 和 lng 值,通过与用户的接近度来检索用户。自从迁移到新服务器 $_POST 以来,它给我带来了很多问题。我的所有脚本在某些方面都工作正常,这始终是一个问题,但现在我所有的 $_POST 调用都返回空值。非常感谢任何和所有帮助?下面的详细信息显示了我所知道的所有信息。我的虚拟主机是 fastwebhost.com,如果有人有使用它们的经验并且知道他们是否有我不知道的设置,我将非常有兴趣了解更多信息。

我正在运行 php 5.5。 curl 命令是:

`curl -H "Content-Type: application/json" -X POST -d '{"lat":121.1234, "lng":141.1234}' http://www.mywebsite.com/test.php

返回以下内容:

nullYou have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '*pi()/180 )*COS(abs(lat)*pi()/180)
          *POWER(SIN((-lng)*pi()/180/2),2))) '
`

我在 php 脚本中使用的代码如下:

<?php 
//connection
    $con = mysqli_connect("localhost","username","password");
    if (!$con) {
        die('Connect Error: ' . mysqli_connect_error());
    }
//select db
$select_db = mysqli_select_db($con, "datbasename") or die ("cant find table");

$tableName = "users";
$origLat = $_POST['lat'];
$origLon = $_POST['lng'];
echo json_encode($origLon); //is returning null everytime
$dist = 20; // This is the maximum distance (in miles) away from $origLat, $origLon in which to search


$query = "SELECT username, profile, image_url, lat, lng, 3956 * 2 * 
          ASIN(SQRT( POWER(SIN(($origLat - abs(lat))*pi()/180/2),2)
          +COS($origLat*pi()/180 )*COS(abs(lat)*pi()/180)
          *POWER(SIN(($origLon-lng)*pi()/180/2),2))) 
          as distance FROM $tableName WHERE 
          lng between ($origLon-$dist/abs(cos(radians($origLat))*69)) 
          and ($origLon+$dist/abs(cos(radians($origLat))*69)) 
          and lat between ($origLat-($dist/69)) 
          and ($origLat+($dist/69)) 
          having distance < $dist ORDER BY distance limit 100;";

    $statement = mysqli_prepare($con, $query)  or die(mysqli_error($con));;
    mysqli_stmt_execute($statement);

    mysqli_stmt_store_result($statement);
    mysqli_stmt_bind_result($statement, $username, $profile, $image_url ,$lat, $lng, $distance);


  $rows = array();
  while(mysqli_stmt_fetch($statement)){
     $row = array();
     $row[username] = $username;
     $row[profile] = $profile;
     $row[image_url] = $image_url;
     $row[distance] = $distance;
     array_push($rows, $row);
  }
  echo json_encode($rows);

mysqli_stmt_close($statement);
mysqli_close($con)
?>  

最佳答案

通常,POST 请求的 POST 正文会有一个键/值结构,这正是您的代码 $_POST['lat'] 所期望的,其中 'lat' 作为键,并将其设置为正文。当您发送 json 作为正文时,它不会自动将 json 内的键/值结构转换为普通的 $_POST 全局变量,因此您必须自己执行此操作。

尝试这样的代码从帖子正文中获取 json 值。

$post = file_get_contents('php://input');
$json_post = json_decode($post, true);
echo $json_post['lat'] . "\n";
echo $json_post['lng'] . "\n";

关于php - 向 php $_POST 发送 cURL 终端命令返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33847017/

相关文章:

mysql - 如何在 mysql 中为具有不相关数据的列添加外键?

PHP MySQL - 内部连接 ​​: userid = part of value

php - 数据库和 php 父 id

Mysql 转储还原失败 : Cannot add foreign key constraint

sql - MySQL 中的 NULL(性能和存储)

c++ - 对于想要为 C++ 找到好的调试器的新手,有什么提示吗?

PHP 购物车给出 "Notice: Undefined index"错误

php - for 循环中的解析错误 : syntax error, unexpected T_VARIABLE

visual-studio - 如何使 `OutputDebugString` 消息成为 Visual Studio 输出窗口中的可单击链接?

c++ - 列表迭代器不兼容断言失败