简体   繁体   English

如何为 PHP 中的 Web 服务调用生成 API 密钥

[英]How to generate an API key for a web-service call in PHP

I have a java program to generate an API key that will be later used to make a web-service call using curl_exec($curl).我有一个 java 程序来生成一个 API 密钥,稍后将用于使用 curl_exec($curl) 进行 Web 服务调用。 I'm running this java program manually everytime i need a new APIKEY.每次我需要一个新的 APIKEY 时,我都会手动运行这个 java 程序。 How do I convert this java program to PHP so I can first generate the API key using PHP code and then use the generated APIKEY in the actual request. How do I convert this java program to PHP so I can first generate the API key using PHP code and then use the generated APIKEY in the actual request. I was trying to use curl_exec() to generate the API key but do not have any idea how to do the DataOutputStream or BufferedReader in PHP.我试图使用 curl_exec() 生成 API 密钥,但不知道如何在 PHP 中执行 DataOutputStream 或 BufferedReader。 Any tips or suggestions?有什么提示或建议吗? Any other alternatives that I can use to generate the API key programmatically in PHP?我可以使用任何其他替代方法在 PHP 中以编程方式生成 API 密钥?

The java program is below: java程序如下:

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class MyRestApi {
  public void Apikey() {
    try {
      URL url = new URL(
      "http://c0016.test.cloud.hpe.com:7001/maximo/oslc/apitoken/create?_lid=testuser1&_lpwd=Test@123");
      HttpURLConnection connection = (HttpURLConnection)url.openConnection();
      String json = "{\"expiration\":1440,\"userid\":\"1234567\"}";
      connection.setRequestProperty("Accept", "application/json");
      connection.setRequestProperty("Content-Type", "application/json");
      connection.setDoOutput(true);
      DataOutputStream d = new DataOutputStream(connection.getOutputStream());
      d.writeBytes(json);
      d.flush();
      d.close();
      System.out.println(connection.getResponseCode());
      BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
      String output;
      while ((output = br.readLine()) != null)
      System.out.println(output);
    } catch (MalformedURLException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

  public static void main(String[] args) {
    MyRestApi restapi = new MyRestApi();
    restapi.Apikey();
  }
}

Thanks for replying back.感谢您回复。 I was able to generate the API key by sending a CURL request as below (If someone needs to refer to the solution):我能够通过发送如下 CURL 请求来生成 API 密钥(如果有人需要参考解决方案):

$curl = curl_init();
curl_setopt_array($curl, array(
    CURLOPT_URL => "http://c0016.test.cloud.hpe.com:7001/maximo/oslc/apitoken/create?_lid=testuser1&_lpwd=Test@123",
    CURLOPT_ENCODING => "gzip,deflate",
    CURLOPT_TIMEOUT => 20,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => "POST",
    CURLOPT_HTTPHEADER => array('Accept: application/json' , 'Content-Type: application/json'),
    CURLOPT_POSTFIELDS => "{\"expiration\":60,\"userid\":\"1234567\"}",
    CURLOPT_RETURNTRANSFER => true,
));
$response = curl_exec($curl);
$results = json_decode($response, true);
curl_close($curl);

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

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