How to send input from HTML Form and use it in php CURL function

陌路散爱 提交于 2021-02-19 08:07:31

问题


I'm new to web programming and I was looking to find the solutions on the internet but I get stuck, so anyone can help me I would appreciate his effort very much.

I have two files. The first file is an HTML form like this.

<html>
<title>index.html</title>
<body>

<form action="index.php" method="post">
Title <input type="text" name="title" /> <br>
Cat <input type="text" name="cat" /> <br>
Private <input type="text" name="private" /> <br>
Description <textarea name="description" rows="10" cols="40"></textarea>  <br>
Size <input type="text" name="size"  /> <br>
RON <input type="text" name="RON" /> <br>
Price <input type="text" name="price" /> <br>
Price NO <input type="text" name="price2"  /> <br>
Stare <input type="text" name="used" /> <br>
Nume <input type="text" name="name"  /> <br>
city <input type="text" name="city" /> <br>
Email <input type="text" name="email"  /> <br>
ON <input type="text" name="on" /> <br>
<input type="submit" name="submit" value="submit" /><br>
</form>



</body>
</html>

The second file is index.php and have this code.

<?php
 $title = $_POST['title'];
 $cat = $_POST['cat'];
 $private = $_POST['private'];
 $description = $_POST['description'];
 $size = $_POST['size'];
 $RON = $_POST['RON'];
 $price = $_POST['price'];
 $price2 = $_POST['price2'];
 $used = $_POST['used'];
 $name = $_POST['name'];
 $city = $_POST['city'];
 $email = $_POST['email'];
 $on = $_POST['on'];


//create array of data to be posted

$post_data = array(
'data[title]' => $title,
'data[category_id]' => $cat,
'data[private_business]' => $private,
'data[description]' =>  $description,
//'data[description]' => 'Description: Pantofi de lac, pantofi , pantofi Pantofi de lac, pantofi , pantofi Pantofi de lac, pantofi , pantofi ',
'data[param_size]' => $size,
'data[param_price][currency]' => $RON,
'data[param_price][0]' => $price,
'data[param_price][1]' => $price2,
'data[param_state]' => $used,
'data[person]' => $name,
'data[city]' => $city,
'data[email]' => $email,
'data[accept]' => $on
);


//traverse array and prepare data for posting (key1=value1)
foreach ( $post_data as $key => $value) {
    $post_items[] = $key . '=' . urlencode($value);
}

//create the final string to be posted using implode()
$post_string = implode ('&', $post_items);

//create cURL connection
$curl_connection =
  curl_init('http://olx.ro/i2/adauga-anunt/');

//set options


curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($curl_connection, CURLOPT_USERAGENT,
  "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.17 Safari/537.36");
curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_connection, CURLOPT_HEADER, 0);
curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1);

//$headers = array();

//$headers[] = 'Host: olx.ro';
//$headers[] = 'User-Agent:"Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:35.0) Gecko/20100101 Firefox/35.0';
//$headers[] = 'Accept: */* ';
//$headers[] = 'Accept-Language: en-US,en;q=0.5';
//$headers[] = 'Accept-Encoding: gzip, deflate';
$headers[] = 'Content-Type: application/x-www-form-urlencoded; charset=UTF-8';
//$headers[] = 'X-Requested-With: XMLHttpRequest';
//$headers[] = 'Referer: http://olx.ro/i2/adauga-anunt/';
//$headers[] = 'Content-Length: 594';
//$headers[] = 'Cookie: PHPSESSID=otsg9lalsh5m06faaviuqtmih4; xtvrn=$542850$540516$; optimizelySegments=%7B%221067457090%22%3A%22ff%22%2C%221087063106%22%3A%22none%22%2C%221060248541%22%3A%22direct%22%2C%221042136847%22%3A%22false%22%2C%221092431561%22%3A%22none%22%2C%221079661599%22%3A%22ff%22%2C%221027596252%22%3A%22false%22%2C%221069071887%22%3A%22direct%22%7D; optimizelyEndUserId=oeu1425804897718r0.9477511105813824; optimizelyBuckets=%7B%7D; POPUPCHECK=1425891298025; is_tablet=0; __utma=29471969.1470095060.1425804898.1425804898.1425804898.1; __utmb=29471969.61.10.1425804898; __utmc=29471969; __utmz=29471969.1425804898.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); last_used_locations=%5B%7B%22regionId%22%3A%224%22%2C%22cityId%22%3A%2226711%22%2C%22districtId%22%3Anull%2C%22label%22%3A%22Brasov%22%2C%22details%22%3A%22Brasov%22%2C%22source%22%3A%22adding%22%2C%22precision%22%3A%22precision-adding%22%7D%5D; mobile2=desktop; ki_t=1425808476633%3B1425808476633%3B1425808476633%3B1%3B1; ki_r=';
//$headers[] = 'Connection: keep-alive';
//$headers[] = 'Pragma: no-cache';
//$headers[] = 'Cache-Control: no-cache';


curl_setopt($curl_connection, CURLOPT_HTTPHEADER, $headers);



//set data to be posted
curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string);

//perform our request
$result = curl_exec($curl_connection);

//show information regarding the request
print_r(curl_getinfo($curl_connection));
echo curl_errno($curl_connection) . '-' .
                curl_error($curl_connection);

if(curl_errno($curl_connection))
{
    echo 'error:' . curl_error($curl_connection);
}

//close the connection
curl_close($curl_connection);
 return $result;
?>

I'm trying to use the HTML form to send the values to curl $post_data (array) and CURL to do his job and post to the $url provided. I tested with echo $_POST('title') and the php page is receiving the input data but CURL don't execute it and I can't figure out why.


回答1:


I'm getting a success response with this:

<?php
/*
 $title = $_POST['title'];
 $cat = $_POST['cat'];
 $private = $_POST['private'];
 $description = $_POST['description'];
 $size = $_POST['size'];
 $RON = $_POST['RON'];
 $price = $_POST['price'];
 $price2 = $_POST['price2'];
 $used = $_POST['used'];
 $name = $_POST['name'];
 $city = $_POST['city'];
 $email = $_POST['email'];
 $on = $_POST['on'];
*/



//create array of data to be posted

$post_data = array(
    'data[title]' => $_POST['title'],
    'data[category_id]' => $_POST['cat'],
    'data[private_business]' => $_POST['private'],
    'data[description]' =>  $_POST['description'],
//'data[description]' => 'Description: Pantofi de lac, pantofi , pantofi Pantofi de lac, pantofi , pantofi Pantofi de lac, pantofi , pantofi ',
    'data[param_size]' => $_POST['size'],
    'data[param_price][currency]' => $_POST['RON'],
    'data[param_price][0]' => $_POST['price'],
    'data[param_price][1]' => $_POST['price2'],
    'data[param_state]' => $_POST['used'],
    'data[person]' => $_POST['name'],
    'data[city]' => $_POST['city'],
    'data[email]' => $_POST['email'],
    'data[accept]' => $_POST['on']
);


//traverse array and prepare data for posting (key1=value1)
foreach ( $post_data as $key => $value) {
    $post_items[] = $key . '=' . urlencode($value);
}

//create the final string to be posted using implode()
//$post_string = implode ('&', $post_items);

//create cURL connection
$curl_connection =
    curl_init();


//set options

curl_setopt($curl_connection, CURLOPT_URL, 'http://olx.ro/i2/adauga-anunt/');
curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($curl_connection, CURLOPT_USERAGENT,
    "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.17 Safari/537.36");
curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_connection, CURLOPT_HEADER, 0);
curl_setopt($curl_connection, CURLOPT_HTTPHEADER, array("Content-type: multipart/form-data"));
curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl_connection, CURLOPT_PUT, 1);
curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_data);

//$headers = array();

//$headers[] = 'Host: olx.ro';
//$headers[] = 'User-Agent:"Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:35.0) Gecko/20100101 Firefox/35.0';
//$headers[] = 'Accept: */* ';
//$headers[] = 'Accept-Language: en-US,en;q=0.5';
//$headers[] = 'Accept-Encoding: gzip, deflate';
//$headers[] = 'Content-Type: application/x-www-form-urlencoded; charset=UTF-8';
//$headers[] = 'X-Requested-With: XMLHttpRequest';
//$headers[] = 'Referer: http://olx.ro/i2/adauga-anunt/';
//$headers[] = 'Content-Length: 594';
//$headers[] = 'Cookie: PHPSESSID=otsg9lalsh5m06faaviuqtmih4; xtvrn=$542850$540516$; optimizelySegments=%7B%221067457090%22%3A%22ff%22%2C%221087063106%22%3A%22none%22%2C%221060248541%22%3A%22direct%22%2C%221042136847%22%3A%22false%22%2C%221092431561%22%3A%22none%22%2C%221079661599%22%3A%22ff%22%2C%221027596252%22%3A%22false%22%2C%221069071887%22%3A%22direct%22%7D; optimizelyEndUserId=oeu1425804897718r0.9477511105813824; optimizelyBuckets=%7B%7D; POPUPCHECK=1425891298025; is_tablet=0; __utma=29471969.1470095060.1425804898.1425804898.1425804898.1; __utmb=29471969.61.10.1425804898; __utmc=29471969; __utmz=29471969.1425804898.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); last_used_locations=%5B%7B%22regionId%22%3A%224%22%2C%22cityId%22%3A%2226711%22%2C%22districtId%22%3Anull%2C%22label%22%3A%22Brasov%22%2C%22details%22%3A%22Brasov%22%2C%22source%22%3A%22adding%22%2C%22precision%22%3A%22precision-adding%22%7D%5D; mobile2=desktop; ki_t=1425808476633%3B1425808476633%3B1425808476633%3B1%3B1; ki_r=';
//$headers[] = 'Connection: keep-alive';
//$headers[] = 'Pragma: no-cache';
//$headers[] = 'Cache-Control: no-cache';


//curl_setopt($curl_connection, CURLOPT_HTTPHEADER, $headers);



//set data to be posted
curl_setopt ($curl_connection, CURLOPT_POST, 1);

//perform our request
$result = curl_exec($curl_connection);


//show information regarding the request
print_r(curl_getinfo($curl_connection));
echo curl_errno($curl_connection) . '-' .
    curl_error($curl_connection);

if(curl_errno($curl_connection))
{
    echo 'error:' . curl_error($curl_connection);
} else {
    echo "Success";
}

//close the connection
curl_close($curl_connection);
return $result;
?>



回答2:


In this example you are using GET method and using CURLOPT_POSTFIELDS.

To use POST keep your existing code but add

 curl_setopt ($curl_connection, CURLOPT_POST, 1);

before the line

    //set data to be posted
curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string);

If you want to use the GET remove:

//set data to be posted
curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string);

update to

    enter code he//create cURL connection
$curl_connection =
  curl_init('http://olx.ro/i2/adauga-anunt?'.$post_string);


来源:https://stackoverflow.com/questions/28975434/how-to-send-input-from-html-form-and-use-it-in-php-curl-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!