如何从 HTML 表单发送输入并在 php CURL 函数中使用它
How to send input from HTML Form and use it in php CURL function
我是网络编程的新手,我想在互联网上寻找解决方案,但我遇到了困难,所以任何人都可以帮助我,我非常感谢他的努力。
我有两个文件。第一个文件是这样的 HTML 形式。
<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>
第二个文件是index.php并且有这个代码。
<?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=28500516$; 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;
?>
我正在尝试使用 HTML 表单将值发送到 curl $post_data
(数组)和 CURL 来完成他的工作,并将 post 发送到 $url
假如。我用 echo $_POST('title')
进行了测试,php 页面正在接收输入数据,但 CURL 不执行它,我不明白为什么。
在此示例中,您使用 GET 方法并使用 CURLOPT_POSTFIELDS。
要使用 POST,请保留现有代码,但添加
curl_setopt ($curl_connection, CURLOPT_POST, 1);
行前
//set data to be posted
curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string);
如果要使用 GET 删除:
//set data to be posted
curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string);
更新为
enter code he//create cURL connection
$curl_connection =
curl_init('http://olx.ro/i2/adauga-anunt?'.$post_string);
我收到了成功的回复:
<?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=28500516$; 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;
?>
我是网络编程的新手,我想在互联网上寻找解决方案,但我遇到了困难,所以任何人都可以帮助我,我非常感谢他的努力。
我有两个文件。第一个文件是这样的 HTML 形式。
<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>
第二个文件是index.php并且有这个代码。
<?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=28500516$; 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;
?>
我正在尝试使用 HTML 表单将值发送到 curl $post_data
(数组)和 CURL 来完成他的工作,并将 post 发送到 $url
假如。我用 echo $_POST('title')
进行了测试,php 页面正在接收输入数据,但 CURL 不执行它,我不明白为什么。
在此示例中,您使用 GET 方法并使用 CURLOPT_POSTFIELDS。
要使用 POST,请保留现有代码,但添加
curl_setopt ($curl_connection, CURLOPT_POST, 1);
行前
//set data to be posted
curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string);
如果要使用 GET 删除:
//set data to be posted
curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string);
更新为
enter code he//create cURL connection
$curl_connection =
curl_init('http://olx.ro/i2/adauga-anunt?'.$post_string);
我收到了成功的回复:
<?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=28500516$; 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;
?>