dirkgroenen / pinterest-api-php

A PHP wrapper for the official Pinterest API. :pushpin:

Home Page:https://developers.pinterest.com/docs/getting-started/introduction/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

->getRateLimitRemaining() returns 'unknown'

gordielachance opened this issue · comments

Hi,
->getRateLimit() works;
but
->getRateLimitRemaining() returns unknown...

Hmm that's weird, getRateLimit() and getRateLimitRemaining() both use the same mechanism to fetch the header which gives us that information. Just confirmed with the Pinterest API Docs and nothing seems to have changed.

I'm not in the position right now to actually check it, but code-wise is should work.

Is there any additional information you can provide?

dumping the $header var in the two appropriate functions in Pinterest.php shows that no headers are being returned - ie. the $header var comes up null.

It seems that the getRateLimit is working because it is a default value of 1000 if no header exists:
return (isset($header['X-Ratelimit-Limit']) ? $header['X-Ratelimit-Limit'] : 1000);

So, neither of those functions are actually working.

Interesting, thanks @zigstum.
Feel free to open a PR if you feel like solving this. Otherwise I'll see if I can make some time soon to fix it.

Running the API call through 'Postman' shows that the headers are present in the response:

Access-Control-Allow-Origin →*
Age →0
Cache-Control →private
Connection →keep-alive
Content-Length →131
Content-Type →application/json
Date →Thu, 16 Nov 2017 17:49:17 GMT
Pinterest-Generated-By →
Pinterest-Version →1294124
X-Content-Type-Options →nosniff
X-Pinterest-RID →XXXX
X-Ratelimit-Limit →200
X-Ratelimit-Remaining →25

Dumping the value of $this->headers in Request.php shows that the headers do exist:

// Get headers from last request
        $this->headers = $ch->getHeaders();
        dd($this->headers);

// this gives:

array:16 [
  0 => "HTTP/1.1 200 OK\r"
  "Access-Control-Allow-Origin" => "*"
  "Age" => "0"
  "Cache-Control" => "private"
  "Content-Type" => "application/json"
  "Pinterest-Version" => "1294124"
  "X-Content-Type-Options" => "nosniff"
  "X-Pinterest-RID" => "XXXX"
  "X-Ratelimit-Limit" => "200"
  "X-Ratelimit-Remaining" => "159"
  "Content-Length" => "369"
  "Date" => "Thu, 16 Nov 2017 18:43:01 GMT"
  "Connection" => "keep-alive"
  "Pinterest-Generated-By" => ""
  1 => "\r"
  2 => ""
]

So, the headers are received, but they are not propagating to the function that is called.

Hello,

First of all, thank you for the great API.

Found the same issue myself, getRateLimitRemaining() returns unknown.

Also, I got rate limited today, so this info would be great to have. :)

Regards,
Szabi.

Well, I have investigated a bit more on this issue, and it seems that getRateLimitRemaining() works, but only if you make a direct API call to any endpoint, before calling it.

So, if you initialize the framework and immediately check getRateLimitRemaining(), it will return 'unknown'.

Seems like the correct headers are used (X-Ratelimit-Remaining). Anybody who can confirm this is indeed a code issue, or if it's related to @sfatfarma's explanation?

The getRateLimitRemaining() method uses the headers from its last made request. If you initialize the framework and immediately check the remaining limit you will indeed get the unknown value.

I guess this could be improved in the documentation, or maybe add something to the unknown value so users know that they should make a request first.

Thank you for the explanations. :)

commented

I had the same problem. I fixed it with a change to capitalization. The headers I got were this:

X-RateLimit...

In the code it has:
X-Ratelimit...

The lowercase L is correct according to Pinterest documentation, but not according to what actually got returned when dumping $pinterest->request;

commented

Actually it seems like it is sometimes one and sometimes the other, so you have to check both.

This will be in release 0.2.12