php - 如何使用 cURL 修复表单并从 api 获取响应
问题描述
我制作了一个 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"];
?>
解决方案
试试这个代码。让我看看你的 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>";
}
?>
推荐阅读
- php - 如何为select2多选添加值
- cuda - 多个进程可以共享一个 CUDA 上下文吗?
- node.js - Lambda Axios 发布到第二个 API 奇怪的异步行为
- reactjs - 如何在反应中显示下拉框其映射值而不是其键
- rest - 将我的 Postman 调用转换为 Dart/Flutter API 调用
- angular - 模块“AppModule”导入的意外值“AngularFireAuth”。请在删除未使用的页面时添加@NgModule 注释
- mysql - mySql 中的 Varcar(200) 与 html textarea 的 maxLength=200 属性不匹配
- reactjs - 在 react-grid-layout 中同时进行垂直和水平压缩
- javascript - 用于在鼠标悬停时修改表格单元格颜色的 Javascript - javascript 的语法错误 800A03EA
- javascript - 为什么我需要两次调用 setPageFromHash() 方法?