使用cURL自动填写并提交表单

时间:2012-06-16 15:33:46

标签: php forms curl webforms

我正在尝试使用cURL自动填写并提交表单,代码在这里,但它绝对没有做任何事情,

<?php
$post_data['countryID'] = '162';
$post_data['autocomplete'] = 'Assam, Karimganj';
$post_data['state'] = '3007';
$post_data['city'] = '60695';
$post_data['countryStateCityCase'] = '2';
$post_data['usesAutoComplete'] = '1';
$post_data['categoryParent'] = '185';
$post_data['categoryChild'] = '219';
$post_data['listing_type'] = '2';
$post_data['title'] = 'Title goes here';
$post_data['currency_typeC'] = '13';
$post_data['priceC'] = '2500';
$post_data['description']= 'Descriptio goes hereDescriptio goes hereDescriptio goes hereDescriptio goes here';
$post_data['email']= 'olx@test.com';
$post_data['ad_language_id']= '1';
$post_data['identity']= '1';
$post_data['imageQty']= '0';
$post_data['phone']= '03311234567';
$post_data['bddisclaimer']= '0';
$post_data['security_code']= 'undefined';
$post_data['captchaKey']= 'undefined';
foreach ( $post_data as $key => $value) {
    $post_items[] = $key . '=' . $value;
}
$post_string = implode ('&', $post_items);
echo $post_string;
$curl_connection =
  curl_init('http://www.olx.in/posting.php?src=8');

curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($curl_connection, CURLOPT_USERAGENT,
  "Mozilla/4.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);

curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string);

$result = curl_exec($curl_connection);
echo $result;
print_r(curl_getinfo($curl_connection));
echo curl_errno($curl_connection) . '-' .
                curl_error($curl_connection);

curl_close($curl_connection);
?>

所有字段都已正确填写,但没有输出任何内容,我根本没有收到任何确认电子邮件,但手动它工作正常,有人可以帮助我吗?

1 个答案:

答案 0 :(得分:2)

第一个理由是,在构建HTTP POST请求的内容时,您没有执行percent-encoding。您可以使用urlencode(..)函数对键值对的值进行编码。

替换

$post_items[] = $key . '=' . $value;

使用:

$post_items[] = $key . '=' . urlencode($value);