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

标签 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']= '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6a0506122a1e0f191e44090507" rel="noreferrer noopener nofollow">[email protected]</a>';
$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);
?>

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

最佳答案

让我头疼的第一个原因是你没有做 percent-encoding同时构建 HTTP POST 请求的内容。您可以使用urlencode(..)函数对键值对的值进行编码。

替换

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

与:

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

关于php - 使用 cURL 自动填写并提交表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11064793/

相关文章:

php - 如何使用API​​在YouTube上启用“通过广告获利”选项

php - 重写这个重复的 if 语句的最佳方法是什么?

javascript - 如何显示有多少人在我的网站上注册?

css - 如何在汇合的表单中设置CSS类或样式?

javascript - Node.js 相当于此curl 请求

php - 尝试为 PHP 构建 Xdebug 时缺少带有 "Make"的头文件

php - Codeception、架构 Global 和 Suite Helpers/PHP

javascript - 了解提交的表格

curl - 使用 curl 进行 WebSocket 服务器健康检查

c++ - Curl 简单的 https 请求不返回任何 C++