php表单提交转换了JSON格式的URL API来发布该数据,但是数据也不会发布同样通过但无法正常工作的API密钥
$values->email = $_POST['email'];
$values->password = $_POST['password'];
$values->phone = $_POST['phoneno'];
$values->owner_id = $_POST['operator_id'];
$values->bank_name = $_POST['bankname'];
$values->ifsc_code = $_POST['ifsc'];
$values->last_name = $_POST['lastname'];
$values->created_by = 'mobiadmin';
$values->first_name = $_POST['firstname'];
$values->account_type = $_POST['accounttype'];
$values->schema = $_POST['schema'];
$values->merchant_meta = array("id"=> null);
$values->merchant_name = $_POST['merchantname'];
$values->account_number = $_POST['accountno'];
$values->acc_holder_name = $_POST['accountholder'];
$array = array("records"=>array($values));
$json_formate = json_encode($array);
$ch = curl_init('api');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_formate);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'x-api-key:aMqwyQJdA1aqH5GkpG5NR78UyswcHhkEaMZCfrC8',
'Content-Length: ' . strlen($json_formate))
);
$result = curl_exec($ch);
答案 0 :(得分:0)
我将为您提供一个非常简单的示例,说明如何设置我的API并从移动应用程序中调用它,或从中获取信息。我已经发表了一些评论,希望对您的项目有进一步的说明或示例。
API
<?php
// this API just fetch articels from a specific URL and view them as a list
// with numbers between from and to
header("Access-Control-Allow-Origin: https://example.com", false); // allowed addresses
header("Access-Control-Allow-Origin: https://www.example.com", false); // allowed addresses
header("Access-Control-Allow-Headers: access");
header("Access-Control-Allow-Methods: GET POST"); // choose GET|POST
header("Access-Control-Allow-Credentials: true");
header('Content-Type: application/json'); // choose type
$file = "classes/Articles.php";
$limit_from = filter_input(INPUT_GET, "limit_from", FILTER_SANITIZE_NUMBER_INT);
$limit_to = filter_input(INPUT_GET, "limit_to", FILTER_SANITIZE_NUMBER_INT);
$articles = new \common\controller\Articles(BASE);
$list = $articles->getArticles((isset($limit_from)) ? $limit_from : 0, (isset($limit_to)) ? $limit_to : 10);
echo json_encode($list);
从PHP页面使用cURL获取api。 可以是单个页面,函数或方法
// create a new cURL resource
// use this option curl_setopt($ch, CURLOPT_URL, "address"); or put address as
// an parameter in curl_init(example address)
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "https://example.com/api/public/article/name/example-name");
// Example of sending POST|GET data through cURL
// curl_setopt($ch, CURLOPT_POST, 1);
// curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array('postvar1' =>'value1')));
// Here you can add you API key choose Authorization|x-api-key
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Authorization: example-key'
));
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// grab URL and pass it to the browser
$data = curl_exec($ch);
// close cURL resource, and free up system resources
curl_close($ch);
return $data;
尽管这是一个GET API,但您可以轻松地将其转换为POST API。