Hi,
I am in a situation where I cannot use PHP cURL module to POST my form data. I have found a great blog post showing how to POST without using cURL here: HTTP POST from PHP, without cURL
Here's my problem though. When I attempt to send my POST request, the request hits the (other) server but the content (POST data) is not transmitted. The other server does not get the content. HOWEVER, if I do use cURL, it works fine. What am I missing? How can I recreate the cURL HTTP POST request without using cURL?
Here is the cURL code that works ($this->params is just $_POST which contains the form data):
$ch = curl_init($this->url);
$params = http_build_query($this->params);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params );
curl_setopt($ch, CURLOPT_FOLLOWLOCATION ,1);
curl_setopt($ch, CURLOPT_HEADER ,0); // DO NOT RETURN HTTP HEADERS
curl_setopt($ch, CURLOPT_RETURNTRANSFER ,1); // RETURN THE CONTENTS OF THE CALL
$result = curl_exec($ch);
curl_close($ch);
return $result;
And here is my non-cURL version that does not work:
$postStr = http_build_query($this->params);
$opts = array(
'http'=>array(
'method' => $method,
//'header' => 'Content-type: application/x-www-form-urlencoded',
//'header' => 'content-type: multipart/form-data',
'content-type' => 'application/x-www-form-urlencoded',
'content-encoding' => 'UTF-8',
'content' => $postStr
)
);
$context = stream_context_create($opts);
$result = file_get_contents($this->url, false, $context);
return $result;
There are no errors or warnings. Just, the accepting server doesn't appear to the contents. Any ideas?