简体   繁体   中英

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). I'm running this java program manually everytime i need a new APIKEY. 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. Any tips or suggestions? Any other alternatives that I can use to generate the API key programmatically in PHP?

The java program is below:

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 = 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);

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