简体   繁体   English

使用PHP发送POST请求并获取JSON响应?

[英]Send POST Request & Get JSON Response in PHP?

I'm trying to send a POST request via PHP from AJAX . 我正在尝试POST request via PHP from AJAX发送POST request via PHP from AJAX I checked the API with Postman . 我用Postman检查了API。 It is working fine. 一切正常。 But it is not getting executed in PHP. 但是它并没有在PHP中执行。 It is not showing up in Network Tab also. 它也没有显示在“网络”选项卡中。

I saw a lot of samples for making a POST Request in Stack Overflow & tried it. 我看到了很多在Stack Overflow中发出POST请求的示例并进行了尝试。 But I can't figure out where I'm going wrong ? 但是我不知道我要去哪里错了?

I have attached both the JS Code & PHP Code here 我在这里同时附上了JS代码和PHP代码

JavaScript CODE JavaScript代码

function editUser(toid, name, mobile, mail, pin, addr, state, dis, subdis, role, user) {

    $.ajax({
        type: "POST",
        url: "edituser.php",
        dataType: 'html',
        data: {
            id: toid,
            fullname: name,
            phone: mobile,
            email: mail,
            address1: addr,
            state: state,
            district: dis,
            subdistrict: subdis,
            pincode: pin,
            usertype: user,
            role: role,
            token: apptoken,
        },
        success: function (response) {
            visibility(false);
            console.log("Response > > " + response);
            if (response.status == "SUCCESS") {
                swal("Updated User", " Information Updated Successfully!", "success");
            }
            loadData();
        }
    });
}

PHP CODE PHP代码

<?php 

// where are we posting to?
$url = 'http://api.tech.com/api/UpdateUser';

    // what post fields?
    $fields = array(
       'id' => $_POST['id'],
       'fullname' => $_POST['fullname'],
       'phone' => $_POST['phone'],
       'email' => $_POST['email'],
       'address1' => $_POST['address1'],
       'state' => $_POST['state'],
       'district' => $_POST['district'],
       'subdistrict' => $_POST['subdistrict'],
       'pincode' => $_POST['pincode'],
       'usertype' => $_POST['usertype'],
       'role' => $_POST['role'],
    );

    // build the urlencoded data
    $postvars = http_build_query($fields);

    // open connection
    $ch = curl_init();

    $token = $_POST['token'];

    // set the url, number of POST vars, POST data
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, count($fields));
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postvars);

        curl_setopt($ch, CURLOPT_HTTPHEADER, array("AppToken: $token", 
"Content-Type: application/x-www-form-urlencoded"));
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

    // execute post
    $result = curl_exec($ch);

    echo $result;

    // close connection
    curl_close($ch);

?>

UPDATE: 更新:

The request sent to the API ($url) is not showing in the Network Tab. 发送到API($ url)的请求未显示在“网络”选项卡中。 But the request to edituser.php is shown. 但是显示了对edituser.php的请求。

在此处输入图片说明

I feel like a lot of context is missing here but I have done a lot of stuff like this so I will try to help out with what I do understand. 我觉得这里缺少很多背景信息,但是我做了很多类似的事情,所以我会尽力帮助我理解。

Here is what I have gathered so far from your question. 到目前为止,这是我从您的问题中收集到的信息。

  1. You are calling an ajax function in JavaScript that triggers the given PHP code. 您正在用JavaScript调用ajax函数来触发给定的PHP代码。
  2. The JavaScript is successful in calling this code "per the network screenshot." JavaScript成功地按“每个网络屏幕截图”调用了此代码。
  3. The network tab indicates the PHP script returns nothing. 网络标签表明PHP脚本未返回任何内容。

Here are my thoughts on how to move forward. 这是我关于如何前进的想法。

  1. You appear to be sending all data from your client side JavaScript including a secure pin and token. 您似乎正在从客户端JavaScript发送所有数据,包括安全的密码和令牌。 This is a very bad idea as it mean your users can all see this secure pin and steal it for their own nefarious purposes. 这是一个非常糟糕的主意,因为这意味着您的用户都可以看到此安全密码,并出于自己的恶意目的窃取它。 Instead store constants like the secure pin in the php code. 而是将常量(例如安全引脚)存储在php代码中。

  2. If you are expecting to see the curl request in the network tab you are mistaken. 如果希望在网络选项卡中看到卷曲请求,那么您会误解。 The curl request is going from server to server and will never be seen by the client. curl请求从一个服务器发送到另一个服务器,客户端将永远不会看到它。

  3. If the response tab is empty you may have something as simple as a syntax error or some kind of curl error that you are not capturing. 如果“响应”选项卡为空,则可能会出现一些简单的错误,例如语法错误或某种卷发错误,而您没有捕获。

Add some of this to your request for debugging: 将其中一些添加到您的调试请求中:

$result = curl_exec($request);
$response_code = curl_getinfo($request, CURLINFO_HTTP_CODE);
echo 'Response code: ' . $response_code;

if(curl_error($request))
{
  echo '<br />Curl error: ' . curl_error($request);
}

You should see the response code at least in the "response" tab of the ajax call. 您至少应该在ajax调用的“响应”选项卡中看到响应代码。

If you still see nothing make sure your PHP configuration is set up to show all warnings and errors, etc and isn't suppressing the information you need. 如果仍然看不到任何内容,请确保您的PHP配置已设置为显示所有警告和错误等,并且未隐藏所需的信息。

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

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