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 请问一下这个运行结果是怎么来的
  • ¥15 这个复选框什么作用?
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下