Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.9k views
in Technique[技术] by (71.8m points)

KOHA cURL PHP API Implementation

Trying to use cURL in interfacing a PHP application with Koha, is there a cURL approach on doing this? Tried the following;

<?php

    $url = "http://opac.test.com/api/v1/oauth/token";
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    curl_setopt($ch, CURLOPT_USERPWD, "User:Password");
    
    curl_setopt($ch, CURLOPT_POST, 0);
    curl_setopt($ch, CURLOPT_HEADER, 0);

    $data = curl_exec($ch);

    echo $data;
    curl_close($ch);

?>

I got a Bye response.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This was my solution, to anyone who may encounter the same problem:

<?php
    $curl = curl_init();

    curl_setopt_array($curl, [
      CURLOPT_URL => "yourdamain/api/v1/oauth/token",
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_ENCODING => "",
      CURLOPT_MAXREDIRS => 10,
      CURLOPT_TIMEOUT => 30,
      CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
      CURLOPT_CUSTOMREQUEST => "POST",
      CURLOPT_POSTFIELDS => "grant_type=client_credentials&client_id=yourcliedid&client_secret=yoursecretkey",//these you generate from your koha account
      CURLOPT_HTTPHEADER => [
        "content-type: application/x-www-form-urlencoded"
      ],
    ]);

    $response = json_decode(curl_exec($curl));
    $err = curl_error($curl);

    curl_close($curl);
    
    $token = $response->access_token;
    
    
    echo "<pre>";
    print_r($token);
    echo "</pre>";
?>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...