7_R3X
7_R3X

Reputation: 4370

CURL PHP sends blank form

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."\n";

    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)."\n";

    curl_close($curl_connection);

?>

Upvotes: 0

Views: 72

Answers (3)

arkascha
arkascha

Reputation: 42915

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.

Upvotes: 1

Professor Abronsius
Professor Abronsius

Reputation: 33813

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

Upvotes: 0

RomanPerekhrest
RomanPerekhrest

Reputation: 92854

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, '', '&'));
...

Upvotes: 0

Related Questions