2014-02-06 59 views
0

我是使用cURL的新手,所以我需要您的帮助。我的情况是,我想通过form-site(form.php)发送一个form(formular.php)。php curl不发送表单中的发布数据

form.php通过cURL调用formular.php。

这里都是源代码:

form.php的

<?php 
    error_reporting(-1); 
    $url = 'http://localhost/formular.php'; 
    $data = array('customerline' => 'nummer1', 'customerphonenumber' => 'nummer2'); 
    $postString = http_build_query($data, '', '&'); 
    $ch = curl_init(); 
    curl_setopt ($ch, CURLOPT_URL, $url); 
    curl_setopt ($ch, CURLOPT_POST, 2); 
    curl_setopt ($ch, CURLOPT_POSTFIELDS, $postString); 
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 
    $post = curl_exec ($ch); 
    curl_close ($ch); 
    echo $post; 
?> 

formular.php

<?php if(isset($_POST['form'])): ?> 
    Sent! 
<?php else: ?> 
    <form action="http://localhost/formular.php" method="post" name="form"> 
     <input type="text" name="customerline" /> 
     <input type="text" name="customerphonenumber" /> 
     <input type="submit" value="send" name="form" /> 
    </form> 
<?php endif; ?> 

我的问题是,我不知道如何获得文字段落“发送!”。

你能帮我吗?

回答

1

你期待你的,如果条件有一个值对的形式if(isset($_POST['form']))但你是不是在发送卷曲。

你应该补充一点。所以,你的帖子参数应该是:

$data = array(
    'customerline' => 'nummer1', 
    'customerphonenumber' => 'nummer2', 
    'form' => 'value' 
); 
+0

感谢您的输入,现在它的工作,完美!举手 (: –

2

您可以直接传入数组而不将其转换为查询字符串。另外CURLOPT_POST应该是true

尝试:

$url = 'http://localhost/formular.php'; 
$data = array('customerline' => 'nummer1', 'customerphonenumber' => 'nummer2'); 
$ch = curl_init(); 
curl_setopt ($ch, CURLOPT_URL, $url); 
curl_setopt ($ch, CURLOPT_POST, true); 
curl_setopt ($ch, CURLOPT_POSTFIELDS, $data); 
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 
$post = curl_exec ($ch); 
curl_close ($ch); 
echo $post; 

上的选项更多信息http://my1.php.net/curl_setopt