2

I need some advice with this curl script. I am trying to send values from the FORM to the curl script, but seems that the curl script doesn`t receive the info.

Here is the script:

<?php
        if($_SERVER['REQUEST_METHOD'] != 'POST') {
            $self = $_SERVER['PHP_SELF'];
    ?>
                      <form method="post" action="<?php echo $self; ?>" class="form-horizontal">
                          <fieldset>
                                <legend>SMS Contact</legend>
                                <div class="control-group">
                                    <label for="basic" class="control-label">Name and Surname</label>
                                    <div class="controls">
                                        <input type="text" name="name" id="name" disabled class='input-square' value="<?php print $contactname;?> <?php print $contactsurname;?>">
                                    </div>
                                </div>
                                <div class="control-group">
                                    <label for="basic" class="control-label">Mobile No</label>
                                    <div class="controls">
                                        <input type="text" name="mobile" id="mobile" disabled class='input-square' value="<?php echo urlencode($contactmobile);?>">
                                    </div>
                                </div>
                                <div class="control-group">
                                    <label for="textcounter" class="control-label" id="textcounter">Textarea</label>
                                    <div class="controls">
                                        <textarea name="textcounter" id="textcounter" class='input-square span9 counter' data-max="160" rows='6'></textarea>
                                    </div>
                                </div>
                                <div class="form-actions">
                                    <button class="btn btn-primary" type="submit">Send SMS</button>
                                </div>
                          </fieldset>
                        </form>
                        <?php
        } else {

            $mobile = $_POST['mobile'];
            $text = $_POST['textcounter'];
            $username = 'xxxx';
            $password = 'xxxx';

            // Set Timezone
            date_default_timezone_set('Africa/Johannesburg');
            $date = date("m/d/y G.i:s", time());

            // Create Unique ID
            $code = md5(time());
            $newid = $code.$clientid;

            $sql="INSERT INTO sent_itmes (sent_id, sent_message, sent_date, sent_quantity, client_id, sent_mobile)VALUES('$newid', '$text', '$date', '1', '$clientid', '$mobile')";
            $result=mysql_query($sql);

    $url = "http://bulksms.2way.co.za/eapi/submission/send_sms/2/2.0"; // URL to calc.cgi
            $fields = array(
            'site'=>'',
            'username'=>($username),
            'password'=>($password),
            'message'=>urlencode($text),
            'msisdn'=>urlencode($mobile)

                            );
            $fields_string="?";
            //url-ify the data for the POST
            foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
            rtrim($fields_string,'&');

            //open connection
            $ch = curl_init();

            //set the url, number of POST vars, POST data
            curl_setopt($ch,CURLOPT_URL,$url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
            curl_setopt($ch,CURLOPT_POST,count($fields));
            curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);

            //execute post
            ob_start();
            curl_exec($ch);
            ob_end_clean();
            echo '<div class="alert alert-block alert-success">
                                          <a class="close" data-dismiss="alert" href="#">×</a>
                                          <h4 class="alert-heading">Success!</h4>
                                          Your message has been Sent!
                                        </div><br/><br/><a href="search_contact.php" class="btn btn-large btn-round">Search Contact</a>';

            //close connection
            curl_close($ch);
        }
        ?>

When submitting the form, I get the error from response server "No recipients specified", so this means that the form value "mobile" doesnt pass though the value to the curl.

Help please, going off my mind.

3
  • you can send data from CURL, have tested with direct input. Commented Sep 28, 2012 at 9:51
  • have you tried debugging? var_dump($fields_string) to make sure it holds the correct values. and CURLOPT_POST needs a boolean value, no count(). and CURLOPT_POSTFIELDS accepts an array, no need to build the field_string. Commented Sep 28, 2012 at 9:58
  • you should not start post-data with "?" Commented Sep 28, 2012 at 11:47

3 Answers 3

2

Try adding a header that describes the content you post and maybe content you apply

    curl_setopt ( $ch, CURLOPT_HTTPHEADER, array(
        'Content-type: application/x-www-form-urlencoded',
        'Accept: text/html',
        'Expect: ',
    ) );
Sign up to request clarification or add additional context in comments.

Comments

2

use this for sending data

        //open connection
        $ch = curl_init($url);

        curl_setopt($ch,CURLOPT_POST,count($fields));
        curl_setopt($ch,CURLOPT_POSTFIELDS,POSTVARS.$fields_string); // this line is edited..
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION  ,1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);


        //execute post
        ob_start();
        curl_exec($ch);

your order was wrong...

more info http://www.askapache.com/php/sending-post-form-data-php-curl.html

5 Comments

still don`t pass the $mobile data :-(
Nope, the problem is that the script doesn`t pass the "$mobile" value in curl. All fields show but not $mobile.
don't use urlencode for mobile number urlencode($mobile) should be $mobile only.
that`s a negative on the value, nothing goes though.
Found the problem in the form where disabled class='input-square' should be class='input-square'. Just removed disabled
1

When you submit form with disabled field, that field do not sent:

<input type="text" name="mobile" id="mobile" disabled class='input-square' value="<?php echo urlencode($contactmobile);?>">

If you want to send "mobile", you should add hidden field for it. And maybe replace disabled field name

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.