Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

thusfar's avatar

What is the fastest way to make HTTP requests???

I'm new to PHP, coming from JS. I would like to know what is the fastest way to make HTTP requests in PHP???

PHP is synchronous, so that would mean that it doesnt continue executing code until it gets back response when using cURL?? Am I right?? If so, how could I avoid that my website gets slow if http response takes to long?

FYI I'm planning on integrating apps like Intercom or Segment, that's why I have to make a lot of HTTP requests.

UPDATE 1

It is the opposite. I want send data to Intercom to log actions for analytics. User signed up, visited this page, that page, did that, and so on. Basically log all their actions.

To do that I have to send HTTP requests to their API. That's at least one outgoing http request for each route I have. Now, while doing that in php functions (in controller), I dont want that making those http requests affects the loading speed of my views/website. So I want to make sure it won't.

0 likes
8 replies
Thijmen's avatar

You could Cache those responses so that they are faster the next time.

usama.ashraf's avatar

@thebasix you have to keep in mind that with PHP you have multiple threads for multiple connections.

So it's not as if one request will block another until it finishes (as may happen with poorly written Node.js).

bugsysha's avatar

If I understand you correctly then you are asking how to send HTTP request from your site to some other site (it doesn't matter who owns it). And you are worried that you wont slow down the first site I've mentioned? Or I didn't GET you at all :D

nfauchelle's avatar

@thebasix

So I guess the problem is something like this.

You want to load your dashboard page, and on that show stats or some data from Intercom and/or Segment. The problem your running into is while this loads the page is blank because PHP is waiting for responses from those services, and you want to make it more a synchronous so you can return part of the page while your fetching the other data to speed up the page.

Is that the general gist?

If so, then why not do this. Render out most of the page with PHP, but then use javascript to ajax call in that other data. So the page displays fast and then the rest of the data comes in as needed. You could cache it as well if thats possible.

1 like
thusfar's avatar

It is the opposite. I want send data to Intercom to log actions for analytics. User signed up, visited this page, that page, did that, and so on. Basically log all their actions.

To do that I have to send HTTP requests to their API. That's at least one outgoing http request for each route I have. Now, while doing that in php functions (in controller), I dont want that making those http requests affects the loading speed of my views/website. So I want to make sure it won't.

jekinney's avatar

You are right by default php will wait for a response before executing the next line of code.

But there are ways around it. In Laravel you may want to look into events. Fire an event. The event is processed in the background. Other words the rest of your code doesn't wait.

Another suggestion is if your hitting that API for a lot of requests to set up a middleware. You can then fire the event from one place and easily add routes to the middleware.

One draw back to keep in mind is errors. When firing an event error handling isn't as straight forward.

Snapey's avatar

yep, use queuing. Offload the task to a queue and get back to the user.

note that out if the box, the queue is still handled synchronously, giving the illusion of off loading the task, but still blocking.

2 likes

Please or to participate in this conversation.