如何使用api key参数使用curl手动api键发布json数据

时间:2018-04-05 06:19:35

标签: php mysql api curl

如何使用curl和api key参数插入json post数据。

在以下代码中,我尝试在网址中添加apikey参数。

index.php:

$data = array(
    // 'name' => '100236',
 //    'age' => '265',
    'nric' => '123546',
    'id' => '266',
    'fullname'=>'sairam',
    'gender' => 'M',
    'password' => '123546',
    'address'=>'jlnklmanalpoi',
    'postcode' => '502103',
    'state' => 'telangana',
    'contact1'=>'+60123654',
     'email' => 'email@email.com',
    'rank'=>'AM',
   'expirydate'=>'14-07-2017'

);
  $post_json = json_encode($data);

          $api_key = '99999your9999api999keyd4eae7c3';    
       $endpoint = 'http://localhost/apib/data.php?key=.$api_key ';
        $ch = @curl_init();
        @curl_setopt($ch, CURLOPT_POST, true);
        @curl_setopt($ch, CURLOPT_POSTFIELDS, $post_json);
        @curl_setopt($ch, CURLOPT_URL, $endpoint);
        @curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
        @curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $response = @curl_exec($ch);
        $status_code = @curl_getinfo($ch, CURLINFO_HTTP_CODE);
        $curl_errors = curl_error($ch);
        @curl_close($ch);
        echo "curl Errors: " . $curl_errors;
        echo "\nStatus code: " . $status_code;
        echo "\nResponse: " . $response;
?>

am unable to use api key below page :

这是终点data.php

$_POST = json_decode(file_get_contents('php://input'), true);


if (isset($_POST['nric'],$_POST['id'],$_POST['fullname'],$_POST['gender'],$_POST['password'],$_POST['address'],$_POST['postcode'],$_POST['state'],$_POST['contact1'],$_POST['email'],$_POST['rank'],$_POST['expirydate'])) {

 global $db;
$db = mysqli_connect("localhost", "root", "", "ilde");
if($db === false)
{

    die("ERROR: Could not connect. " . mysqli_connect_error());
}

    $nric=mysqli_real_escape_string($db,$_POST["nric"]);
    $id =(int)$_POST["id"];
     $fullname=mysqli_real_escape_string($db,$_POST["fullname"]);
      $gender=mysqli_real_escape_string($db,$_POST["gender"]);

我想手动使用带有apikey的安全网址。请帮我解决这个问题。如何使用curl和api key参数插入json post数据。请帮我下面是我的代码我试图在url中添加apikey参数

1 个答案:

答案 0 :(得分:-1)

尝试以下端点:

$endpoint = 'http://localhost/apib/data.php?key='.$api_key.' ';