Link to home
Start Free TrialLog in
Avatar of mosuncoker
mosuncoker

asked on

What may be wrong with my php http post function

Hello,
I have a php file that sends content to a server. Please see the code snippet below. The curl.exec() wont work except i append the api params to the posted url.  It seems the code works as a Get method and not Post.

See code :


$sender = $_POST['sender'];
 
 
 
$message = $_POST['message'];
//addressGroup
$r_source = $_POST['r_source']; 
if($r_source=="0"){
$receiver = $_POST['receiver'];
}
else{
 // do something 
}
 

$request = "";
 $username7 = $_SESSION['username'];
		$password7 = $_SESSION['password'];
$param["username"] = $username7; //the account username provided by gateway provider
 

$param["filenumber"] = $receiver; //the recipient of the message
 
$param["senderid"] = $sender; //the message sender
 
$param["message"] = $message; //the message that will be sent
 
 
 
 
 
foreach($param as $key=>$val){
 
$request.= $key."=".urlencode($val);
 
$request.= "&";
 
}
 
 
 
$request = substr($request, 0, strlen($request)-1); 
 
 
 
$url = "http://xyz.com/send.ashx?".$request; //the gateway's interface provided by the gateway provider
 
 
 
$ch = curl_init();
 
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);  
 
curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 
$response = curl_exec($ch);

Open in new window

Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

GET and POST are different methods and are intended for different purposes.  If the API expects a GET request, it will likely not work well with POST.  This is normal and expected behavior.  So I think to make this work you would omit these settings:
curl_setopt($ch, CURLOPT_POST, true);  
curl_setopt($ch, CURLOPT_POSTFIELDS, $request);

Open in new window

Avatar of mosuncoker
mosuncoker

ASKER

It works as get here.
Could you give me a code snippet for php http post
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial