我正在尝试使用cUrl发布表单。
我正在使用PHP,但我想发布的网站是使用aspx。过去两天我一直在浏览互联网寻求解决方案,我觉得我走得很远,但它仍然没有用。我有隐藏的字段和asp会话ID,我还设法发送相关的cookie。 它不是https网站,因此不需要SSL验证,也不涉及代理服务器或任何形式的身份验证。
Curl不会返回任何错误。
手动发布后,同一页面返回一些附加信息,这些是我需要的结果。但是使用curl,结果与仅包含表单的页面相同。 该页面确实需要cookie,但我认为我解决了这个问题,因为我不再注意到这个问题了。
但是,firebug Net面板不会显示任何Post数据。也许curl不发布任何内容?
这是我的代码:
$fields = array(
'ToolkitScriptManager_HiddenField' => '',
'__EVENTTARGET' => '',
'__EVENTARGUMENT' => '',
'__VIEWSTATE' => $viewstate, // when using urlencode() or rawurlencode(), i get message: Object moved to here, which links to an error page.
'__EVENTVALIDATION' => $eventval,
'scrollY' => 460
);
// get ASP.NET_SessionId from cookie file (need to do this because cookie file doen not contain AcceptCookie=Accept=True)
// set the cookies we need
// looks like we only need 2 relevant cookies: ASP.NET_SessionId and AcceptCookie
$cookie_string = 'SC_ANALYTICS_GLOBAL_COOKIE=64d9c94544d04fb6ac221265e99cb914; mf_user=1; __utma=66847866.1614717554.1414684101.1415373395.1415378138.6; __utmz=66847866.1414684101.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); ';
$cookie_string .= 'AcceptCookie=Accept=True; ' . 'ASP.NET_SessionId=' .getAspSessionidFromCookiefile($cookies) . ';';
var_dump($cookie_string);
curl_setOpt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields); // can be passed as array or not ??
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:33.0) Gecko/20100101 Firefox/33.0");
// curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($ch, CURLOPT_REFERER, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: text/html;',
'Connection: Keep-Alive',
'Host: www.website.com'
));
// curl_setopt($ch,CURLOPT_HEADER,true);
// curl_setopt($ch,CURLINFO_HEADER_OUT,true);
// curl_setopt($ch, CURLOPT_COOKIEFILE, $cookies); // gives notice 'this site requires cookies'
curl_setopt($ch, CURLOPT_COOKIE, $cookie_string); // no notice, but still not get the result page
// $cookie = 'SC_ANALYTICS_GLOBAL_COOKIE=64d9c94544d04fb6ac221265e99cb914; mf_user=1; __utma=66847866.1614717554.1414684101.1415373395.1415378138.6; __utmz=66847866.1414684101.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); AcceptCookie=Accept=True; ASP.NET_SessionId=hz3yt4blbfaiv2eyd4zds155;';
// curl_setopt($ch, CURLOPT_COOKIE, $cookie); // no notice about cookies, but cookie content probably not good, so try to rewrite cookie data as string
$result = curl_exec($ch);
echo 'Error: ';
var_dump(curl_error($ch)); // returns: Error: string(0)
echo ' - Code: ';
var_dump(curl_errno($ch)); // returns: Code: int(0)
echo ' - curl getinfo: ';
var_dump(curl_getinfo($ch),true);
echo ' result = <br/><br/><br/>';
var_dump($result);
//close connection
curl_close($ch);
非常感谢任何帮助。