简体   繁体   English

php 将 json 数据发布到 api 中

[英]php to post json data into api

php form submit converted the JSON formate the URL API to post that data but data will not post API key also passed but not working 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);

I will give you an very simple example how I set up my API and call it from mobile apps or where you would like to fetch from.我会给你一个非常简单的例子,我如何设置我的 API 并从移动应用程序调用它或者你想从哪里获取。 I've put in some comments and hope this will give you some clarification or examples to further in your project.我已经发表了一些评论,希望这会给您一些说明或示例,以进一步推进您的项目。

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);

Fetch api with cURL from PHP page.从 PHP 页面使用 cURL 获取 api。 Could be a single page, function or a method可以是单个页面、函数或方法

// 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;

Though this is an GET API you could easley turn it into a POST API.尽管这是一个 GET API,但您可以 easley 将其转换为 POST API。

Read more about https://php.net/manual/en/function.http-build-query.php阅读更多关于https://php.net/manual/en/function.http-build-query.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM