Mendeley search catalog pagination

81 views Asked by At

I have written a code in PHP to query the search catalog in Mendeley and retrieve data for a specific journal. However, the problem is that the results are limited until 100. The code is below and is written in PHP. How can I add mendeley pagination after 100 results?

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

There are 1 answers

0
Martin Marinov On

According to Mendeley Pagination documentation the pagination URL can be obtained from the Link field of the HTTP response header

I will use curl as a prototyping tool to illustrate the usage of Mendeley Pagination.

In your example running

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'

Produces the following headers

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

So the next page can be resolved using URL

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