Reputation: 6883
Simply put, I need to make a POST request to a web service using a php script. The problem is that the php version on the server is 4.4.x and curl is disabled. Any ideas how I can make the call and read the response?
Upvotes: 0
Views: 243
Reputation: 471
you could basically use socket (fsockopen) and fputs like this :
$port = 80;
$server = "domain.com";
$valuesInPost = 'param=value&ahah=ohoho';
$lengthOfThePost = strlen($valuesInPost);
if($fsock = fsockopen($server, $port, $errno, $errstr)){
fputs($fsock, "POST /path/to/resource HTTP/1.1 \r\n");
fputs($fsock,"Host: $server \r\n");
fputs($fsock,"User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13 \r\n");
fputs($fsock,"Accept-Language: fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3 \r\n");
fputs($fsock,"Keep-Alive: 115 \r\n");
fputs($fsock,"Connection: keep-alive\r\n");
fputs($fsock,"Referer: http://refererYou.want\r\n");
fputs($fsock,"Content-Type: application/x-www-form-urlencoded\r\n");
fputs($fsock,"Content-Length: $lengthOfThePost\r\n\r\n");
fputs($fsock,"$valuesInPost\r\n\r\n");
$pcontent = "";
// results
while (!feof($fsock))
$pcontent .= fgets($fsock, 1024);
// echoes response
echo $pcontent;
}
There might be some syntax errors due to like rewriting.
Note you can use the port you want.
Upvotes: 1
Reputation: 238115
You can use fopen
and stream_context_create
, as per the example on the stream_context_create
page:
$context = stream_context_create(array(
'http' => array (
'method' => 'GET'
)
));
$fp = fopen ('http://www.example.com', 'r', $context);
$text = '';
while (!feof($fp)) {
$text .= fread($fp, 8192);
}
fclose($fp);
Also, see HTTP context options and Socket context options to see the options you can set.
Upvotes: 2