使用Curl将数据发送到PHP的问题

时间:2012-09-28 09:45:57

标签: php

我需要一些关于这个curl脚本的建议。我正在尝试将值从FORM发送到curl脚本,但似乎curl脚本没有收到信息。

这是脚本:

<?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);
        }
        ?>

提交表单时,我从响应服务器“未指定收件人”中收到错误,因此这意味着表单值“mobile”不会将值传递给curl。

请帮帮忙,离开我的脑海。

3 个答案:

答案 0 :(得分:2)

用它来发送数据

        //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);

你的订单错了......

更多信息http://www.askapache.com/php/sending-post-form-data-php-curl.html

答案 1 :(得分:2)

尝试添加描述您发布的内容的标题以及可能适用的内容

    curl_setopt ( $ch, CURLOPT_HTTPHEADER, array(
        'Content-type: application/x-www-form-urlencoded',
        'Accept: text/html',
        'Expect: ',
    ) );

答案 2 :(得分:1)

当您提交带有禁用字段的表单时,该字段不会发送:

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

如果要发送“移动”,则应为其添加隐藏字段。也许可以替换禁用的字段名称