使用cURL通过jQuery Ajax将数据作为JSON发送到PHP Rest API

时间:2018-11-12 22:57:12

标签: php jquery json rest api

我需要将一些数据发送到REST API,并包括一个自定义标头值。我尝试回显通过cURL脚本发送的数据,但出现空白页。 这是我的HTML

<form id="RestData" action=Processor.php" method="POST">
    <ul>
        <li>
            <input type="text" id="email" name="email">
        </li>
        <li>
            <input type="submit" value="Update User" id="">
        </li>
    </ul>
</form>

我的jQuery:

$(function(){
    $('#RestData').validate({
        errorPlacement: function(error, element) {},
        submitHandler: function(form) {
            $.ajax({
                type:$('#RestData').attr('method'),
                url: form.action,
                data: {
                    email = $('#email').val();
                },
                beforeSend: function(data){
                    console.log(data);
                },
                success: function(data){
                    console.log(data);
                    if(data.type == "error") {
                        //error handling
                    } else if(data.type == "success"){
                        //success handling
                    } 
                }
            });
                return false;
        },
        rules: {
            email: {
                required:true,
                email:true
            }
        }
    });
});

在这一点上,当我通过PHP处理器将其回显时,我的json数组周围出现了双引号

<?php
    $email = $_POST['email'];
    $expired = NULL;
    $funded = TRUE;

    $data = array(
        "email" => $email,
        "expired" => $expired,
        "funded" => $funded
    );
    echo json_encode($data);
?>

像这样-> {“ email”:“ test@test.com”,“ expired”:null,“ funded”:true} 我不知道这是否会成为问题,是否有必要删除任何想法?

使用我的最终cURL代码,什么都没有发生-黑屏,并且网络面板中也没有响应。

<?php
    $email = $_POST['email'];
    $expired = NULL;
    $funded = TRUE;

    $data = array(
        "email" => $email,
        "expired" => $expired,
        "funded" => $funded
    );

    $url = 'https://rest.api';

    $json_string = json_encode($data);

    $headers = array (
        "Content-Type: application/json; charset=utf-8",
        "Content-Length: " .strlen($json_string),
        "Authorization: Bearer long token here"    
    );

    $channel = curl_init($url);
    curl_setopt($channel, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($channel, CURLOPT_CUSTOMREQUEST, "PUT");
    curl_setopt($channel, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($channel, CURLOPT_POSTFIELDS, $json_string);
    curl_setopt($channel, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($channel, CURLOPT_CONNECTTIMEOUT, 10);

    curl_exec($channel);
    $statusCode = curl_getInfo($channel, CURLINFO_HTTP_CODE);
    curl_close($channel);    
    return $statusCode;
    echo $json_string;
?>

这有点不尽如人意,我经常使用cURL,只是不使用rest API。我知道我应该得到响应,并试图在页面上回显它以进行调试,但我只是得到一个空白页面。

1 个答案:

答案 0 :(得分:2)

您没有在变量中获得cURL响应。

第二,您正在return语句后执行echo,它将永远不会执行。

<?php
    $email = $_POST['email'];
    $expired = NULL;
    $funded = TRUE;

    $data = array(
        "email" => $email,
        "expired" => $expired,
        "funded" => $funded
    );

    $url = 'https://rest.api';

    $json_string = json_encode($data);

    $headers = array (
        "Content-Type: application/json; charset=utf-8",
        "Content-Length: " .strlen($json_string),
        "Authorization: Bearer long token here"    
    );

    $channel = curl_init($url);
    curl_setopt($channel, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($channel, CURLOPT_CUSTOMREQUEST, "PUT");
    curl_setopt($channel, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($channel, CURLOPT_POSTFIELDS, $json_string);
    curl_setopt($channel, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($channel, CURLOPT_CONNECTTIMEOUT, 10);

    $response = curl_exec($channel);
    $statusCode = curl_getInfo($channel, CURLINFO_HTTP_CODE);
    curl_close($channel);

    http_response_code($statusCode);
    if ( $statusCode != 200 ){
       echo "Status code: {$statusCode} \n".curl_error($channel);
    } else {
       echo $response;
    }
?>