简体   繁体   English

我正在尝试使用PHP Curl填写表格

[英]I am trying to fill in a form by using PHP Curl

I am working on a little project. 我正在做一个小项目。 For this project in need to fill in a form on a third party website. 对于此项目,需要在第三方网站上填写表格。 I tried it with my code showed below, can someone please tell me what i am doing wrong? 我尝试使用下面显示的代码进行尝试,有人可以告诉我我做错了吗? I am trying it on this form: 我正在尝试这种形式:

        <form name="contactform" method="post" action="send_form_email.php">
        <table width="450px">
        <tr>
         <td valign="top">
          <label for="first_name">First Name *</label>
         </td>
         <td valign="top">
          <input  type="text" name="first_name" maxlength="50" size="30">
         </td>
        </tr>
        <tr>
         <td valign="top">
          <label for="last_name">Last Name *</label>
         </td>
         <td valign="top">
          <input  type="text" name="last_name" maxlength="50" size="30">
         </td>
        </tr>
        <tr>
         <td valign="top">
          <label for="email">Email Address *</label>
         </td>
         <td valign="top">
          <input  type="text" name="email" maxlength="80" size="30">
         </td>
        </tr>
        <tr>
         <td colspan="2" style="text-align:center">
          <input type="submit" value="Submit"> 
         </td>
        </tr>
        </table>
        </form>

And this is my PHP code 这是我的PHP代码

$url = '[domain]';
$fields = array(
'lname' => urlencode($_POST['$last_name']),
'fname' => urlencode($_POST['$first_name']),
'title' => urlencode($_POST['$title']),
'company' => urlencode($_POST['$institution']),
'age' => urlencode($_POST['$age']),
'email' => urlencode($_POST['$email']),
'phone' => urlencode($_POST['$phone'])
);

//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_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);

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

//close connection
curl_close($ch);

Thanks for the help! 谢谢您的帮助!

try like that to see curl errors: 尝试像这样查看卷曲错误:

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
if(curl_errno($ch)){
    echo 'Request Error:' . curl_error($ch);
}else var_dump($result);

also, consider using http_bulid_query instead of this mess. 另外,请考虑使用http_bulid_query而不是这种混乱情况。

$data = array(
    'foo' => 'bar',
    'baz' => 'boom',
    'cow' => 'milk',
    'php' => 'hypertext processor'
);

echo http_build_query($data) . "\n";

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

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