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

mosuncokerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
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

0
mosuncokerAuthor Commented:
It works as get here.
Could you give me a code snippet for php http post
0
Ray PaseurCommented:
I thought you said the server worked with GET requests but not with POST requests?  

Anyway, here is my teaching example of how to make a POST request via cURL:
<?php // demo/curl_post_example.php

/**
 * Demonstrate how to use cURL to make a POST request
 * This may be used to start an asynchronous process
 *
 * http://php.net/manual/en/book.curl.php
 */
error_reporting(E_ALL);


function curl_post($url, $post_array=array(), $timeout=2, $error_report=TRUE)
{
    // PREPARE THE POST STRING
    $post_string = NULL;
    foreach ($post_array as $key => $val)
    {
        $post_string .= $key . '=' . urlencode($val) . '&';
    }
    $post_string = rtrim($post_string, '&');

    // PREPARE THE CURL CALL
    $curl = curl_init();
    curl_setopt( $curl, CURLOPT_URL,            $url           );
    curl_setopt( $curl, CURLOPT_HEADER,         FALSE          );
    curl_setopt( $curl, CURLOPT_POST,           TRUE           );
    curl_setopt( $curl, CURLOPT_POSTFIELDS,     $post_string   );
    curl_setopt( $curl, CURLOPT_ENCODING,       'gzip,deflate' );
    curl_setopt( $curl, CURLOPT_TIMEOUT,        $timeout       );
    curl_setopt( $curl, CURLOPT_RETURNTRANSFER, TRUE           );
    curl_setopt( $curl, CURLOPT_FOLLOWLOCATION, TRUE           );
    curl_setopt( $curl, CURLOPT_FAILONERROR,    TRUE           );

    // EXECUTE THE CURL CALL
    $htm = curl_exec($curl);
    $err = curl_errno($curl);
    $inf = curl_getinfo($curl);

    // ON FAILURE
    if (!$htm)
    {
        // PROCESS ERRORS HERE
        if ($error_report)
        {
            echo "CURL FAIL: $url TIMEOUT=$timeout, CURL_ERRNO=$err";
            echo "<pre>\n";
            var_dump($inf);
            echo "</pre>\n";
        }
        curl_close($curl);
        return FALSE;
    }

    // ON SUCCESS
    curl_close($curl);
    return $htm;
}


// USAGE EXAMPLE CREATES ASSOCIATIVE ARRAY OF KEY=>VALUE PAIRS
$args["name"] = 'Ray';
$args["mail"] = 'Ray.Paseur@Gmail.com';

// ACTIVATE THIS TO SEE THE ARRAY OF ARGS
// var_dump($args);

// SET THE URL
$url = "http://Iconoun.com/demo/bounce_post.php";

// CALL CURL TO POST THE DATA
$htm = curl_post($url, $args, 3, TRUE);

// SHOW WHAT CAME BACK, IF ANYTHING
if ($htm)
{
    echo "<pre>";
    echo htmlentities($htm);
}
else
{
    echo "NO RESPONSE YET FROM $url -- MAYBE BECAUSE IT IS RUNNING ASYNCHRONOUSLY";
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.