门德利搜索目录分页

时间:2017-08-23 13:18:10

标签: php pagination catalog mendeley

我在PHP中编写了一个代码来查询Mendeley中的搜索目录并检索特定日记的数据。但是,问题是结果有限,直到100.代码如下,用PHP编写。如何在100个结果后添加mendeley分页?

function auth(){

$client_id = '';
$client_secret = '';
$redirect_uri = '';

if(isset($_GET['code'])) {

    if(!isset($_SESSION['access_token'])) {
        # Authorize
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,"https://api.mendeley.com/oauth/token");
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/x-www-form-urlencoded;"));
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array('client_id'=>$client_id, 'client_secret'=>$client_secret, 'code'=>$_GET['code'],'redirect_uri'=>$redirect_uri, 'grant_type'=>'authorization_code')));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, TRUE); 
        //curl_setopt ($ch, CURLOPT_CAINFO, getcwd()."\cacert.pem");
        $server_output = curl_exec ($ch);
        $errmsg  = curl_error($ch) ;
        if($errmsg) {
            var_dump($errmsg);
        }

        curl_close ($ch);

        $result = json_decode($server_output, true);
        // var_dump($result);
        $_SESSION['access_token'] = $result['access_token'];
    }
    # Call the main function
    main();

}

function getResponse($path) {

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://api.mendeley.com/".$path);

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Authorization: Bearer '.$_SESSION['access_token'],
    'Accept: application/vnd.mendeley-document.1+json'
));

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, TRUE); 
//curl_setopt ($ch, CURLOPT_CAINFO, getcwd()."\cacert.pem");

$server_output = curl_exec ($ch);
$responseCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$header_len = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header = substr($server_output, 0, $header_len);
$body = substr($server_output, $header_len);

$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);

$errmsg  = curl_error($ch) ;
if($errmsg){
    var_dump($errmsg);  
}

curl_close ($ch);

return json_decode($server_output, true);

}


    function main(){


  $search_terms = 'Academy of Management Journal';

$min_year='2012';
$max_year='2016';



    $result = getResponse('/search/catalog?source='.urlencode($search_terms).'&min_year='.$min_year.'&max_year='.$max_year.'&limit=2' . '&view=stats' );
    var_dump($result);

    if(isset($result)) {
        if(isset($res['id'])) {
        $data[] = getResponse('/catalog/'.$res['id']);
        var_dump($data);
       sleep(2);
    }



}

1 个答案:

答案 0 :(得分:0)

根据Mendeley Pagination documentation,可以从HTTP响应标头的<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="500" height="500" style="shape-rendering:geometricPrecision; text-rendering:geometricPrecision; image-rendering:optimizeQuality; fill-rule:evenodd; clip-rule:evenodd" version="1.0"> <rect style="fill:none;stroke:#373435;stroke-width:2.08346" width="500" height="500"></rect> <text fill="#000000" x="219" y="109" text-anchor="middle" font-size="90.75" font-family="Twine">Hello</text> <text fill="#000000" x="207" y="180" text-anchor="middle" font-size="90.75" font-family="Twine">Tetst</text> </svg>`字段获取分页URL

我将使用curl作为原型工具来说明Mendeley分页的用法。

在您的示例中运行

Link

生成以下标题

curl -s -D - 'https://api.mendeley.com//search/catalog?source=Academy%20of%20Management%20Journal&min_year=2012&max_year=2016' -H "Authorization: Bearer $ACCESS_TOKEN" -H 'Accept: application/vnd.mendeley-document.1+json'

因此可以使用URL

解析下一页
HTTP/1.1 200 OK
Content-Type: application/vnd.mendeley-document.1+json
Date: Tue, 29 Aug 2017 15:13:34 GMT
Link: <https://api.mendeley.com/search/catalog?marker=00000000-0000-000a-0000-000000000000&limit=10&source=Academy%20of%20Management%20Journal&min_year=2012&reverse=false&max_year=2016&order=asc>; rel="next"
Mendeley-Count: 4185202
Vary: Accept-Encoding
Vary: Accept-Encoding
X-Mendeley-Trace-Id: Anv9p0YeJtY
Content-Length: 17779
Connection: keep-alive