我试图通过PHP CURL发布表单。 这是我使用的代码:(已编辑)
// This is some data in the $postfields, cut of version because there is a lot of fields
$postdata = array(
// Fields with a name starting with '_D:/' are hidden and set to one space char
'_dyncharset' => 'UTF-8', '_dynSessConf' => '-7211106995117675809', '/claims/BrgClaimFormhandler.value.firstName' => 'John',
'_D:/claims/BrgClaimFormhandler.value.firstName' => ' ', '/claims/BrgClaimFormhandler.value.email' => 'johndoe@example.com',
'_D:/claims/BrgClaimFormhandler.value.email' => ' ', '/claims/BrgClaimFormhandler.value.telephone' => '555 783 4574',
'_D:/claims/BrgClaimFormhandler.value.telephone' => ' ' // ....
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.hyatt.com/hyatt/specials/offers/brgClaimPage.jsp");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // Allow for redirects.
curl_setopt($ch, CURLOPT_TIMEOUT, 500); // Set the curl functions timeout.
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0); // Set the connect timeout to wait indefinitely.
curl_setopt($ch, CURLOPT_USERAGENT, "Opera/9.80 (Windows NT 5.1) Presto/2.12.388 Version/12.15");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Suspend output.
curl_setopt($ch, CURLOPT_COOKIEJAR, 'C:\tmp\hyatt_cookies.txt'); // Save cookies to this file.
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: text/html, application/xml;q=0.9, application/xhtml+xml, image/png, image/webp, image/jpeg, image/gif, image/x-xbitmap, */*;q=0.1',
'Accept-Language: hr-BA,hr;q=0.9,en;q=0.8', 'Accept-Encoding: utf-8', 'Connection: Keep-Alive',
));
$j = curl_exec($ch); // Execute the curl command.
curl_close($ch);
unset($ch);
// Second request:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // Allow for redirects.
curl_setopt($ch, CURLOPT_TIMEOUT, 500); // Set the curl functions timeout.
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0); // Set the connect timeout to wait indefinitely.
curl_setopt($ch, CURLOPT_USERAGENT, "Opera/9.80 (Windows NT 5.1) Presto/2.12.388 Version/12.15");
curl_setopt($ch, CURLOPT_COOKIEFILE, 'C:\tmp\hyatt_cookies.txt');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Suspend output.
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postdata));
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: text/html, application/xml;q=0.9, application/xhtml+xml, image/png, image/webp, image/jpeg, image/gif, image/x-xbitmap, */*;q=0.1',
'Accept-Language: hr-BA,hr;q=0.9,en;q=0.8', 'Accept-Encoding: utf-8', 'Connection: Keep-Alive',
'Content-Type: multipart/form-data; boundary=----------dLk3HzGmthYEYQtSHNptND; charset=UTF-8'));
$h = curl_exec($ch);
curl_close($ch);
echo $h;
页面显示在输出中,但仅在首次访问页面时没有成功发布,没有关于无效或缺少字段的消息。
当我手动发布这些数据时,我获得了成功,当我故意省略一些数据时,我会收到有关所需字段缺失消息的表单。
但是,当我通过此代码执行此操作时,我只是显示带有表单的页面以输入数据而没有消息丢失或无效。
我还可以使用curl_opt()或其他一些技术来设置其他技术吗?
答案 0 :(得分:0)
最有可能的问题是该网站会检查Cookie。这个过程主要包括两个阶段:
因此,您必须在脚本中重现此过程,并启用Cookie自动处理(通过CURLOPT_COOKIEFILE
/ CURLOPT_COOKIEJAR
选项)。
这意味着你必须做两个卷曲请求:
BTW,curl_getinfo()
应在 curl_exec
致电后被称为。