【问题标题】:How can I check curl api key is authorized or not in reciever side如何在接收方检查 curl api 密钥是否已授权
【发布时间】:2018-04-09 04:14:06
【问题描述】:

大家好,我正在使用 curl 将数据从一个网站推送到另一个网站,它现在工作得很好,但我想在接收方 url 中添加一个令牌或 apikey 或密钥。我如何在接收方代码中写入。令牌或 apikey 或密钥是否匹配。但 apikey 或令牌不是来自数据库。 下面是我的代码

此代码在发送端:使用 curl 和参数 apikey

    $payload = json_encode(array("users" => $json['users']));
     $post_json = json_encode($content);     
         $apikey = 'OTM2NTQ0MwMTA3MDYxMQNDAxOTU2MwMTA4MDQ1MgMzIzMDAyMA';    
           $endpoint = 'http://localhost/apib/data.php?apikey='.$api_key;
            $ch1 = @curl_init();
            @curl_setopt($ch1, CURLOPT_POST, true);
            @curl_setopt($ch1, CURLOPT_POSTFIELDS, $payload);
            @curl_setopt($ch1, CURLOPT_URL, $endpoint);
            @curl_setopt($ch1, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
            @curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);
            $response = @curl_exec($ch1);
            $status_code = @curl_getinfo($ch1, CURLINFO_HTTP_CODE);
            $curl_errors = curl_error($ch1);
            @curl_close($ch1);
            /*echo "curl Errors: " . $curl_errors;
            echo "\nStatus code: " . $status_code;
            echo "\nResponse: " . $response;*/
            echo "\nResponse: " . $response;

在其他网站的数据如下:我如何在这里为apikey编写代码

   \$jfile = file_get_contents('php://input');
    $final_res = json_decode($jfile, true) ;
    $dataaa =  $final_res['users'];
    $apikey;  
    global $db;
    $db = mysqli_connect("localhost", "root", "", "apis");
    if($db === false){
      die("ERROR: Could not connect. " . mysqli_connect_error());
    }

现在我想通过 api key 发送数据..如何在接收端写入是否正确

【问题讨论】:

  • 您可以使用初始值加密您的数据,然后将 HMAC 附加到它。如果做得正确,这将确保您的数据没有被从表单网页 A 到网页 b 篡改。网页 A 和 B 共享相同的密钥。我会推荐使用 libSodium 加密库。这是一个讨论这个的链接。 paragonie.com/blog/2015/05/…
  • no no .i just want like apikey in senderside 应该匹配 apikey 接收方
  • 请看我更新的答案。

标签: php rest api curl


【解决方案1】:

更新

对于页面 A,让我们在发送之前将您的 api 密钥添加到您的 json 数组中。像这样:

$myData = array("users" => $json['users']);

$key = 'OTM2NTQ0MwMTA3MDYxMQNDAxOTU2MwMTA4MDQ1MgMzIzMDAyMA';

$api_key = array('apikey' => $key);

$myData = $myData + $api_key;

$payload = json_encode($myData);

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

$ch1 = @curl_init();
        @curl_setopt($ch1, CURLOPT_POST, true);
        @curl_setopt($ch1, CURLOPT_POSTFIELDS, $payload);
        @curl_setopt($ch1, CURLOPT_URL, $endpoint);
        @curl_setopt($ch1, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
        @curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);
        $response = @curl_exec($ch1);
        $status_code = @curl_getinfo($ch1, CURLINFO_HTTP_CODE);
        $curl_errors = curl_error($ch1);
        @curl_close($ch1);
        /*echo "curl Errors: " . $curl_errors;
        echo "\nStatus code: " . $status_code;
        echo "\nResponse: " . $response;*/
        echo "\nResponse: " . $response;

如果您只想比较页面 A 和页面 B 的键值,那么您可以对页面 B 执行类似的操作。

B页:

    $api_key = 'OTM2NTQ0MwMTA3MDYxMQNDAxOTU2MwMTA4MDQ1MgMzIzMDAyMA';

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

    print_r($final_res);        

    $dataaa =  $final_res['users'];     


    if($api_key == $final_res['apikey']){        


      echo 'The api key matches.';

      global $db;
      $db = mysqli_connect("localhost", "root", "", "apis");
      if($db === false){
        die("ERROR: Could not connect. " . mysqli_connect_error());
      }

    } else { 

      echo 'The api key does not match.';
      exit();

    }

这是一种不好的做法,因为它看起来好像您的 api 密钥是不变的。任何看到 api 密钥的人都可以随意使用它来做任何页面 B 所做的事情。这里没有真正的保障。如果您的 api 密钥用于其他任何可能很重要的事情,您可以将其分享给全世界。

如果您按照我在评论中的建议进行操作,则 api 密钥可以在页面 A 和 B 之间共享,但在发布请求中附加到 api 密钥的实际值会在每次发送时发生变化。但是您将能够解密它,并且它始终是 api 密钥。如果没有,那就有问题了。此外,只要您的密钥没有被泄露,您将能够保证您发送的任何其他数据也没有被篡改。

强烈建议您阅读此链接:

https://paragonie.com/blog/2015/05/using-encryption-and-authentication-correctly

要做你正在做的事情实际上并没有那么困难,你只需要熟悉你选择的库和所需的方法。

【讨论】:

  • apikey 每次都不会改变。只有一个唯一的 api 密钥。所以我的实际任务是 curl 我需要使用 apikey 作为参数调用
  • 我的解决方案应该满足您对页面 B 的需求。用您的代码对其进行测试并告诉我。
  • 我得到的 apikey 不匹配
  • $api_key = 'OTM2NTQ0MwMTA3MDYxMQNDAxOTU2MwMTA4MDQ1MgMzIzMDAyMA'; if($api_key == $final_res['apikey']){ echo 'api键匹配。';全局$db; $db = mysqli_connect("localhost", "root", "", "apis"); if($db === false){ die("错误:无法连接。" .mysqli_connect_error()); } } else { echo 'API 键不匹配。';出口(); }
  • 看我更新的答案,你应该把api键变量放入页面A的json数组中,用于“$payload”变量
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-12
  • 2011-09-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多