[英]php to post json data into api
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);
我會給你一個非常簡單的例子,我如何設置我的 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,但您可以 easley 將其轉換為 POST API。
閱讀更多關於https://php.net/manual/en/function.http-build-query.php
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.