Paddy
Paddy

Reputation: 2863

What's the best way to submit a POST request from PHP?

I've been trying to find an answer to this, and can't seem to. What's the best way to submit a simple POST request to another server from PHP? cURL seems to be running excruciatingly slow; I'm getting several seconds of loading, waiting on cURL. Any ideas?

EDIT: On request, here's the code:

  $x = curl_init("http://www.server.com/API.php");
  curl_setopt($x, CURLOPT_POST, 1);
  curl_setopt($x, CURLOPT_POSTFIELDS, $inputdata);
  $data = curl_exec($x);
  curl_close($x);
  echo $data;

It was, however, in a loop. I'm assuming that simple oversight was probably the source of my problems.

Upvotes: 0

Views: 364

Answers (3)

LostInTheCode
LostInTheCode

Reputation: 1744

Sockets are a bit low leveled, but pretty fast. You can also try HttpRequest: http://php.net/manual/en/function.httprequest-send.php

However, in my opinion, I don't think it's so much cURL as your server connection. Try setting this to log and see what's the problem:

curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_VERBOSE, true); 

And check your DNS settings, and that of the site you're connecting to. You can try and load the page manually, and see how fast you do it on your connection and your browser. You can also try getting the latest edition of cURL and any other libraries you use with it. If you're making multiple requests, use a cURL multi-handle, instead of several separate handles. And if you don't need the body of the webpage, and just submitting something, try NO_BODY to true, that would cut down loading time a lot. Lastly, try caching or multithreading.

EDIT: And also try not setting so many headers or cookies, those drag your speed down massively. And if you really want us to work on a good answer, post your code and I'll comment on where I see improvements can be made. And by the way, if you have ipv6 enabled in your cURL build and don't use it, disable it, I hear of lag issues with ipv6. Other peculiar problems I can remember include not having a reverse DNS set up on your server.

Upvotes: 3

netcoder
netcoder

Reputation: 67695

I usually use fopen() http wrapper, with stream_context_create(). See this comment for an example.

Upvotes: 1

KeatsKelleher
KeatsKelleher

Reputation: 10191

You could use sockets:

http://www.jonasjohn.de/snippets/php/post-request.htm

Upvotes: 1

Related Questions