简体   繁体   中英

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

   $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. 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. 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.

Read more about https://php.net/manual/en/function.http-build-query.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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