我是网络编程的新手,我想在互联网上找到解决方案,但我遇到困难,所以任何人都可以帮助我,我非常感谢他的努力。
我有两个文件。第一个文件是这样的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=$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;
?>
我正在尝试使用HTML表单将值发送到curl $post_data
(数组)和CURL来完成他的工作并发布到提供的$url
。我测试了echo $_POST('title')
并且php页面正在接收输入数据,但CURL没有执行它,我无法弄清楚原因。
答案 0 :(得分:1)
在此示例中,您使用的是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);
答案 1 :(得分:1)
我得到了成功回应:
<?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;
?>