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

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);

?>

图片转代码服务由CSDN问答提供 功能建议

我创建了一个名为question_control.php的表单,其中包含5个字段,question,op1,op2,op3和op4。 表单接受参数并将其发送到object.php,然后object.php将数据以类对象的形式写入文件。

 &lt; form action =“  object.php“method =”GET“&gt; 
   
 
 

我尝试将数据发送到question_control.php,发现没有数据写入文件。 当我尝试向object.php发送数据时,我发现已经创建了新对象,但所有字段都是空的。 我修改了curl_errno()以查看是否发生了任何错误但它返回0.这是代码

 &lt;?php 
 
 $ curl_connection = curl_init('http:/  /127.0.0.1/project/object.php');
nn curl_setopt($ curl_connection,CURLOPT_CONNECTTIMEOUT,30); 
 curl_setopt($ curl_connection,CURLOPT_USERAGENT,“Mozilla / 25.0(兼容; 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']  ='我'; 
 $ post_data ['op1'] ='11'; 
 $ post_data ['op2'] ='22'; 
 $ post_data ['op3'] ='33'; 
 $  post_data ['op4'] = '44'; 
 
 foreach($ post_data as $ key =&gt; $ value)
 {
 $ post_items [] = $ key。  '='。  $ value; 
} 
 $ post_string = implode('&amp;',$ 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); 
 
?&gt; 
   
 
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

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.

    打赏 评论
  • dongposhi8677 2016-02-07 09:09

    Use http_build_query function to generate urlencoded string for passing into request:

    ...
    $post_data['question'] = 'My';
    $post_data['op1'] = '11';
    $post_data['op2'] = '22';
    $post_data['op3'] = '33';
    $post_data['op4'] = '44';
    curl_setopt($curl_connection, CURLOPT_POSTFIELDS, http_build_query($post_data, '', '&'));
    ...
    
    打赏 评论
  • douxingmou4533 2016-02-07 09:18

    I have perhaps missed the point of the initial setting of the $post_data array with static values but as you are submitting the form via GET why not simply use the values entered in the form?

    <?php
        $ch = curl_init( 'http://127.0.0.1/project/object.php' );
        curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, 30);
        curl_setopt( $ch, CURLOPT_USERAGENT,"Mozilla/25.0 (compatible; MSIE 6.0; Windows NT 5.1)");
        curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt( $ch, CURLOPT_POST, true );
    
        /* automatically generate the required post data using the $_GET array data sent by the form */
        curl_setopt( $ch, CURLOPT_POSTFIELDS, http_build_query( $_GET ) );
    
        $result = curl_exec( $ch );
        $info=curl_getinfo( $ch );
        $errors=curl_errno( $ch );
        curl_close( $ch );
    
        if( !empty( $result ) ) echo $result;
        if( !empty( $info ) ) print_r( $info );
        if( !empty( $errors ) ) print_r( $errors );
    ?>
    
    打赏 评论

相关推荐 更多相似问题