【问题标题】:Post Koha Patrons to Koha LMS using php CURL使用 php CURL 将 Koha Patrons 发布到 Koha LMS
【发布时间】:2021-09-07 09:59:47
【问题描述】:

尝试将顾客发布到 KOHA LMS,但我不断收到错误 400,如下所示;

{
    //Here is missing some code.
    "errors": [{
            "message": "Missing property.",
            "path": "/body/address"
        },
        {
            "message": "Missing property.",
            "path": "/body/category_id"
        },
        {
            "message": "Missing property.",
            "path": "/body/city"
        },
        {
            "message": "Missing property.",
            "path": "/body/library_id"
        },
        {
            "message": "Missing property.",
            "path": "/body/surname"
        }],
    "status": 400
}

我已经尝试了所有我能想到的方法来解决这个错误,而且似乎没有关于 KOHA 集成的具体文档。如果有人让它工作,请提供帮助。

下面是显示curl代码的部分代码;

{
    //Here is missing some code.
    $url2 = "http://example.com:8000/api/v1/patrons";
    $username = "user";
    $password = "pass";
    $data = array(
        'addPatron' => array(
            'address' => "",
            'city' => "",
            'cardnumber' => $num,
            'firstname' => $fname,
            'surname' => $sname,
            'other_name' => $mname,
            'phone' => $phone,
            'email' => $email,
            'category_id' => $category,
            'library_id' => $library_id,
        )
    );


    /*$data = array(                        
                'address' => "",
                'city' => "",
                'cardnumber' => $num,
                'firstname' => $fname,
                'surname' => $sname,
                'other_name' => $mname,
                'phone' => $phone,
                'email' => $email,
                'category_id' => $category,
                'library_id' => $library_id                     
            );*/



    $ch3 = curl_init($url2);

    curl_setopt($ch3, CURLOPT_USERPWD, $username. ":".$password);
    curl_setopt($ch3, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch3, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch3, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch3, CURLOPT_POSTFIELDS, http_build_query($data));

    $headers = array();
    $headers[] = 'Cookie: CGISESSID='.$cookies['CGISESSID'];
    $headers[] = 'Pragma: no-cache';
    $headers[] = 'Upgrade-Insecure-Requests: 1';
    curl_setopt($ch3, CURLOPT_HTTPHEADER, $headers);

    $d = curl_exec($ch3);
    curl_close($ch3);

    echo "<pre>";
    print_r(json_decode($d));
    echo "</pre>";
}

上面的代码基本上试图将 Koha 顾客发布到图书馆管理系统中。到目前为止,我能够获得顾客。事实证明,使用相同的场景发布是很困难的。对答案的任何帮助或提示都非常重要赞赏。

【问题讨论】:

    标签: php api web-services curl


    【解决方案1】:

    根据https://demo.bibkat.no/api/v1/.html#op-post-patrons 上的文档(顺便说一下,您应该在问题中链接到您的 API 文档),此 API 需要 JSON 请求,但您使用 http_build_query() 发送 application/x-www-form-urlencoded 请求

    替换

        curl_setopt($ch3, CURLOPT_POSTFIELDS, http_build_query($data));
    

        curl_setopt($ch3, CURLOPT_POSTFIELDS, json_encode($data));
    

    并添加

        $headers[] = 'Content-Type: application/json';
    

    也替换

        curl_setopt($ch3, CURLOPT_CUSTOMREQUEST, "POST");
    

        curl_setopt($ch3, CURLOPT_POST, 1);
    

    引用 libcurl CURLOPT_CUSTOMREQUEST 文档@https://curl.se/libcurl/c/CURLOPT_CUSTOMREQUEST.html

    许多人错误地使用此选项将整个请求替换为自己的请求,包括多个标头和 POST 内容。虽然这在许多情况下可能有效,但它会导致 libcurl 发送无效请求,并且可能会严重混淆远程服务器。使用 CURLOPT_POST 和 CURLOPT_POSTFIELDS 设置 POST 数据。

    同样严格来说,你应该向api维护者报告错误,当接收到Content-Type: application/x-www-form-urlencoded而期望JSON不是Http 400 Bad Request时的正确响应(虽然它很接近),正确的响应是

    HTTP 415 不支持的媒体类型

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-10
      • 2016-08-29
      • 2022-01-15
      • 1970-01-01
      • 2010-10-23
      • 2015-05-29
      • 2020-04-26
      相关资源
      最近更新 更多