简体   繁体   English

PHP Curl不会发送POST请求

[英]PHP Curl wont send POST request

i have a html page that submits 2 forms(formone & formtwo) via ajax on one button click. 我有一个html页面,单击一次按钮即可通过ajax提交2个表单(formone和formtwo)。 formone is submitted to formone.php and if it was succesfully sent formtwo is submitted to formtwo.php. 将formone提交给formone.php,如果成功发送,则将formtwo提交给formtwo.php。 Everything works fine. 一切正常。 Except i need to send data via POST to another php script (on another server, but for now i'm testing it on the same server). 除非我需要通过POST将数据发送到另一个php脚本(在另一台服务器上,但是现在我正在同一台服务器上对其进行测试)。 I tried it with the following code but it wont work (i don't get any errors though). 我用以下代码尝试了它,但是它不起作用(尽管我没有任何错误)。

Curl code i used! 我使用的卷曲代码!

function transferData()
{
//Set up some vars
$url = 'test.php';
$user = 'sampletext';
$pw = 'sampletext';

$fields = array(
            'user'=>urlencode($user),
            'pw'=>urlencode($pw)
        );

// Init. string
$fields_string = '';
// URL-ify stuff
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_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);

//execute post
$result = curl_exec($ch);

//close connection
curl_close($ch);
}

This is the code for my ajax form submission: 这是我的ajax表单提交的代码:

function submitforms()
{
    FormSubmissionOne();
    function FormSubmissionOne() 
    {
        //Form 1
        var $form = $('#formone');
        $.ajax(
        {
            type: 'POST',
            url: $form.attr('action'),
            data: $form.serialize(),
            success: function (msg) 
            {
                FormSubmissionTwo();
            },
            error: function(msg) 
            {
             alert(msg);
            }
        });
    }
    function FormSubmissionTwo() 
    {
        //Form 2
        var $form2 = $('#formtwo'); 
        $.ajax(
        {
            type: 'POST',
            url: $form2.attr('action'),
            data: $form2.serialize(),
            success: function (msg) 
            {
                alert(msg);
                //redirection link 
                window.location = "test.php";
            }
        });
    }       
}

This is test.php (receiving script from curl function) 这是test.php(从curl函数接收脚本)

  $one = $_POST['user'];
  $two = $_POST['pw'];

  echo "results:";
  echo $one;
  echo "\r\n";
  echo $two;
  echo "\r\n"; 

There are a few issues, firstly, CURLOPT_POST is for a boolean not a count. 有几个问题,首先, CURLOPT_POSTboolean而不是计数。

So change this: 所以改变这个:

curl_setopt($ch,CURLOPT_POST,count($fields));

to

curl_setopt($ch,CURLOPT_POST, 1); // or true

Secondly, you need to tell CURL that you want the returned data. 其次,您需要告诉CURL您想要返回的数据。 You do that using CURLOPT_RETURNTRANSFER 您可以使用CURLOPT_RETURNTRANSFER

So your curl related code should look like this: 因此,与curl相关的代码应如下所示:

//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, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);

//execute post
$result = curl_exec($ch);
print_r($result); // just see if result
//close connection
curl_close($ch);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM