druybew06513 2016-02-07 08:51
浏览 49
已采纳

CURL PHP发送空白表单

I created a form named question_control.php which has 5 fields, question, op1,op2,op3 and op4. The form takes the parameter and sends it to object.php which in turn writes the data to a file in the form of objects of a class.

    <form action="object.php" method="GET">

I tried sending the data to question_control.php and found that no data is written to the file. When I tried sending data to object.php I found that new object has been created but all the fields are empty. I tred curl_errno() to see if any error occured but it returns 0. Here's the code

<?php

    $curl_connection = curl_init('http://127.0.0.1/project/object.php');

    curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 30);
    curl_setopt($curl_connection, CURLOPT_USERAGENT,"Mozilla/25.0 (compatible; MSIE 6.0; Windows NT 5.1)");
    curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1);

    $post_data['question'] = 'My';
    $post_data['op1'] = '11';
    $post_data['op2'] = '22';
    $post_data['op3'] = '33';
    $post_data['op4'] = '44';

    foreach ( $post_data as $key => $value) 
    {
        $post_items[] = $key . '=' . $value;
    }
    $post_string = implode ('&', $post_items);

    print $post_string."
";

    curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string);

    $result = curl_exec($curl_connection);

    print $result;

    print_r(curl_getinfo($curl_connection));
    echo curl_errno($curl_connection)."
";

    curl_close($curl_connection);

?>
  • 写回答

3条回答 默认 最新

  • dongyi9298 2016-02-07 10:24
    关注

    As it turned out in the discussion in the comments to the question you actually have to make a GET request, not a POST request.

    So have a try with this approach:

    <?php
    $query_parameters = http_build_query([
        'question' => 'My',
        'op1' => '11',
        'op2' => '22',
        'op3' => '33',
        'op4' => '44'
    ]);
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'http://127.0.0.1/project/object.php?' . $query_parameters);
    // ... more curl_setopt(...) calls ...
    

    So you attach the query parameters to the URL by means of the query separator ? and you do not specify any POST fields for the request. GET is the default for cURL, so nothing more you have to take care of.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题