简体   繁体   中英

Send POST Request & Get JSON Response in PHP?

I'm trying to send a POST request via PHP from AJAX . I checked the API with Postman . It is working fine. But it is not getting executed in 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. But I can't figure out where I'm going wrong ?

I have attached both the JS Code & PHP Code here

JavaScript CODE

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 

// 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. But the request to edituser.php is shown.

在此处输入图片说明

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.
  2. The JavaScript is successful in calling this code "per the network screenshot."
  3. The network tab indicates the PHP script returns nothing.

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. 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.

  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.

  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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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