【问题标题】:how to use api key parameter to post json data with manual api key using curl如何使用 api 密钥参数通过 curl 手动 api 密钥发布 json 数据
【发布时间】:2018-09-14 20:43:35
【问题描述】:

如何插入带有 curl 和 api 键参数的 json post 数据。

在下面的代码中,我试图在 url 中添加一个 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 的安全 url。请帮我解决这个问题。如何使用 curl 和 api 密钥参数插入 json 发布数据。请帮助我下面是我正在尝试在 url 中添加 apikey 参数的代码

【问题讨论】:

  • $endpoint = 'http://localhost/apib/data.php?key=.$api_key ';更改为$endpoint = 'http://localhost/apib/data.php?key='.$api_key;
  • 好的。但是在data.php文件中我如何写api_key是否匹配?
  • 您可以在data.php中通过$_GET['key'];检索密钥
  • $api_key = '99999your9999api999keyd4eae7c3';这是一个api key data.php 给index.php 使用这个key index.php 需要将数据推送到data.php。所以现在我们需要 $api_key = '99999your9999api999keyd4eae7c3';在data.php??
  • 我已经提到你可以通过 data.php 中的 $api_key = $_GET['key'] 获得它

标签: php mysql api curl


【解决方案1】:

尝试以下端点:

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

【讨论】:

  • 好的。但是我只是在 index.php 中手动分配了 api 键,它也应该与 data.php 中的匹配。但是我可以在 data.php 中写入吗。即使我输入不同的键也是如此工作,但我需要唯一的 api 密钥。请帮助我
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-10-20
  • 1970-01-01
  • 2020-10-31
  • 1970-01-01
  • 1970-01-01
  • 2023-01-15
  • 2021-10-07
相关资源
最近更新 更多