Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.3k 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 Reply

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>";
?>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...