Contentful logo

Contentful Community

How does the PHP SDK works for request?

Hi,

I don’t understand how the php SDK works for request.

I dumped a message (“Request”) in the function “requestAndBuild” of Contentful\Delivery\Client, and when loading my website page, i got 173 dumped “Request”…

I can not believe that the SDK made 173 requests to the api only for retrieving a simple content-type with JSON object and couple of images…

Is that normal??

Thanks.

Hi @olivier.castro-perri,

That depends on how exactly you’re using the SDK and how your query looks like. Would you mind sharing it with us? (without any credentials, of course)

Okay i was dumping empty query so those requests was not made, but i still have 38 requests made.

So i guess i new request is made when i try accessing a linked asset or entry?

It depends again on how your query is structured. You can use the includes parameter and the resolveLinks method as that will resolve the content of your linked entries.

Hey Olivier,

I think I mentioned this to you in the past, but the SDK needs to know about locales and content types in order to work correctly. This means that on every server request on your app, it will make requests to Contentful for the equivalent of $client->getEnvironment() (for locales), and then $client->getContentTypes(), trying to merge multiple requests into one, if possible.

This means that if you call $client->getEntries(), the SDK will make:

  • 1 query for locales
  • 1 query for entries
  • 1 query for content types (the SDK inspects the result set of the entries query, looks for content types that are needed, and then only makes one request for those)

Following this, info about locales and content types are still there, so if you call getEntries() again, the SDK will not make further requests (unless new content types are found in the result set).

But because of how PHP works, every request to your app will be a fresh start, and memory will be cleaned after that’s over. This means that the SDK will have to request locales and content types repeatedly, unless you cache these locally as it’s described in this tutorial:
https://www.contentful.com/developers/docs/php/tutorials/caching-in-the-php-cda-sdk/

Finally, in the SDK requestAndBuild also checks the local cache before actually querying the API. Depending of what you do, 38 calls to that function might be normal, but I suspect the vast majority of those actually don’t end up calling the API, they are intercepted way before that.

To reliably know which requests were actually performed, use this snippet after all your other queries have been made:

foreach ($client->getMessages() as $message) {
    $uri = (string) $message->getRequest()->getUri();
    // do something with $uri
}

$client->getMessages() returns an array of Contentful\Core\Api\Message objects, which contain info about request/response, time, and possibly exceptions raised in all requests that effectively were made against Contentful’s API.

As Gabriel sad, you can use $query->setInclude($value) to increase the value of automatically resolved links in the Contentful response, which means the SDK will have those values already stored locally and won’t make extra queries. Try using different values of include (it defaults to 1, and has 10 as max value), and see which one works best for your scenario. I suggest avoiding mindlessly setting 10, because on an uncached query to the API it might take a while to internally resolve all those links, and also you will possibly get a huge result set with a lot of stuff you probably won’t need.

I hope this answers your doubts :slightly_smiling_face:

Davide

1 Like