views:

33

answers:

1

Hi guys,

I have a webservice..just say example.com. I need to do a HTTP POST to call this web service in PHP and send data to this web service. The data I'm posting is from a form so is available in variables. I have read that this might be a way to do it, but I'm not sure, so any pointers in the right direction would be appreciated.

$service_url = 'http://example.com/rest/user';
$curl = curl_init($service_url);
$curl_post_data = array(
$compName,
$package,
$email1,
$telephone1
);

curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post_data);
+1  A: 

Yep that should do the trick. Just execute that curl handle:

$returnData = curl_exec($curl);

Do note that when you're using an array as your POSTFIELDS and not a string "key=val&key2=val..." it will set it to multipart/form-data and not application/x-www-form-urlencoded. I know I've had troubles with that my self at some point.

Birk
@Birk...thanks for the reply. Do I just stick that line in at the end? I'll give it a try and see how I get on. There's really no way for me to post it as a string so I suppose I'll cross that bridge if I come to it :) Thanks!
TaraWalsh
Ya you just throw the curl_exec in at the end. It executes your "curl", with the current settings.
Birk
Thanks a lot Birk :)
TaraWalsh