使用REST API php在KOHA中传递令牌

3phpmpom  于 2023-01-08  发布在  PHP
关注(0)|答案(1)|浏览(146)

我一直试图在Koha的php中使用REST在头文件中传递生成的令牌,这是我一直得到的;

{"error":"Authentication failure."}

想使用下面的代码从koha获得顾客数据;

<?php
    $curl = curl_init();

    curl_setopt_array($curl, [
      CURLOPT_URL => "mykohadomain:8000/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=clientID&client_secret=clientSecret",
      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($response);
    echo "</pre>";
    
    $curl2 = curl_init();

    curl_setopt_array($curl2, [
      CURLOPT_URL => "mykohadomain:8000/api/v1/patrons",
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_ENCODING => "",
      CURLOPT_MAXREDIRS => 10,
      CURLOPT_TIMEOUT => 30,
      CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
      CURLOPT_CUSTOMREQUEST => "GET",
      CURLOPT_HTTPHEADER => [
        "Authorization: Bearer ".$token,
        "Content-Type: application/json"
      ],
    ]);

    $response2 = curl_exec($curl2);
    $err2 = curl_error($curl2);

    curl_close($curl2);
    
    echo "<pre>";
    print_r($response2);
    echo "</pre>";
?>

令牌被正确地打印出来了,但是当在第二次调用GET顾客时传递令牌时,我得到了授权失败。任何人都知道如何解决这个问题。

k0pti3hp

k0pti3hp1#

首先确保启用系统首选项REST API选项,然后
php代码

$url = "http://YOUR_URL/api/v1/patrons";
    $username = "YOUR_STAFF_ADMIN_YOUR_NAME";
    $password = "STAFF_ADMIN_PASSWORD";
    $data = ["surname"=>"xyz"];

    $ch = curl_init($url);

    curl_setopt($ch,CURLOPT_USERPWD, $username . ":" . $password);
    curl_setopt($ch,CURLOPT_CUSTOMREQUEST, "GET");
    curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch,CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch,CURLOPT_POSTFIELDS, http_build_query($data));
    $d = curl_exec($ch);
    curl_close($ch);
    print_r(json_decode($d));

相关问题