如何修复表单并使用cURL从api获取响应

问题描述 投票:-1回答:1

我已经制作了HTML表单,当我进行升级时,我希望它使用API​​并显示结果。

<form action="upgrade.php" method="POST"><input name="k" placeholder="Key" style="border-radius: 4px;background-color: #efefef;border: grey;margin-bottom: 2%;width: 35%;padding: .6rem .75rem;" type="text" /><br />
<label><b>Country</b></label><br />
<br />
<select name="c" style="border-radius: 4px; background-color: #efefef; border: grey; margin-bottom: 2%; width: 35%; padding: .6rem .75rem;"><option value="AR">Argentina Remaining: 4324</select>

<p style="text-align: center;"><b>Invites Remaining: 35068</b></p>
<br />
<input class="btn btn--green-bg" style="margin: 0px;" type="submit" value="Upgrade!" /></form>

代码

<?php 

// $postRequest = array(
    // 'firstFieldData' => 'key',
    // 'secondFieldData' => 'countrycode'
// );

error_reporting(E_ALL);
ini_set('display_errors', 1);


$cURLConnection = curl_init("https://upgrader.cc/API/v2/?key=" . $_POST['k'] ."&country=".$_POST['c']);
//curl_setopt($cURLConnection, CURLOPT_POSTFIELDS, $postRequest);
curl_setopt($cURLConnection, CURLOPT_RETURNTRANSFER, true);

$apiResponse = curl_exec($cURLConnection);
curl_close($cURLConnection);

// $apiResponse - available data from the API request
$jsonArrayResponse = json_decode($apiResponse);
$array = json_decode(json_encode($jsonArrayResponse), True);
$response = @json_decode($abc);
if (($response) && (true === $response->success)) {
    echo $array["data"]["token"];
    echo $array["data"]["address"];
} else {
    echo $array["message"];
    }


?>  

这里是API密钥失败的答案:

{"status":"failure","message":"Failure Reason"}

和成功答案:

{"status":"success","data":{"token":"Token Link","address":"Address","used_total":"Used Total","useable":True / False}}

当我使用此代码时,我得到一个答案,但我无法使它成功或仅使一个或另一个失败

<?php 

// $postRequest = array(
    // 'firstFieldData' => 'key',
    // 'secondFieldData' => 'countrycode'
// );

error_reporting(E_ALL);
ini_set('display_errors', 1);


$cURLConnection = curl_init("https://upgrader.cc/API/v2/?key=" . $_POST['k'] ."&country=".$_POST['c']);
//curl_setopt($cURLConnection, CURLOPT_POSTFIELDS, $postRequest);
curl_setopt($cURLConnection, CURLOPT_RETURNTRANSFER, true);

$apiResponse = curl_exec($cURLConnection);
curl_close($cURLConnection);

// $apiResponse - available data from the API request
$jsonArrayResponse = json_decode($apiResponse);
$array = json_decode(json_encode($jsonArrayResponse), True);

echo $array["message"];
?>
php json
1个回答
0
投票

尝试此代码。让我看看您的json响应将是什么。现在就做....

您将需要检查api是发布请求还是获取请求,例如。

<?php
    // set value of k and country...
    $yourkey =$_POST['k'];
    $country =$_POST['c'];


// make sure that key and country is not empty and exit.
    if($yourkey ==''){
echo "key is empty";
exit();
}

    if($country ==''){
echo "country is empty";
exit();
}

    $url_post ="https://upgrader.cc/API/v2/?key=$yourkey&country=$country";
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url_post);
    curl_setopt($curl, CURLOPT_POST, true);

    // Send post request
    curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");

    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $result_post = curl_exec($curl);

    curl_close($curl);

    print_r($result_post);


?>

更新的部分

这里您可以打印json调用返回的消息和状态,避免出现问题

<?php
    // set value of k and country...
    $yourkey =$_POST['k'];
    $country =$_POST['c'];


// make sure that key and country is not empty and exit.
    if($yourkey ==''){
echo "key is empty";
exit();
}

    if($country ==''){
echo "country is empty";
exit();
}

    $url_post ="https://upgrader.cc/API/v2/?key=$yourkey&country=$country";
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url_post);
    curl_setopt($curl, CURLOPT_POST, true);

    // Send post request
    curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");

    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $result_post = curl_exec($curl);

    curl_close($curl);

    //print_r($result_post);



$json = json_decode($result_post, true);
$status = $json['status'];
$message = $json['message'];


if($status == 'failure'){
// print message
echo "Failure message is here: <b>$message</b>";

}


// assuming status value is success
if($status == 'success'){
// print message
echo "success message is here: <b>$message</b>";

}



?>
© www.soinside.com 2019 - 2024. All rights reserved.