如何使用Boundary和PHP CURL执行POST请求
我正在尝试登录使用以下POST表单的网页:`如何使用Boundary和PHP CURL执行POST请求,php,curl,login,php-curl,Php,Curl,Login,Php Curl,我正在尝试登录使用以下POST表单的网页:` <div class="form"> </div> <fieldset class="inlined"> <input type="hidden" name="label" value=""> <input type="hidden" name="textName" value="j_username"> <div class
<div class="form"> </div>
<fieldset class="inlined">
<input type="hidden" name="label" value="">
<input type="hidden" name="textName" value="j_username">
<div class="field required ">
<label for="_content_b2b_eu_login_jcr_content_par_start_j_username">Username</label>
<input type="text" class=" large" id="_content_b2b_eu_login_jcr_content_par_start_j_username" name="j_username" value="" placeholder="" onkeydown="">
</div>
<input type="hidden" name="password" value="j_password">
<div class="field required ">
<label for="_content_b2b_eu_login_jcr_content_par_start_j_password">Password</label>
<input class="large" id="_content_b2b_eu_login_jcr_content_par_start_j_password" type="password" autocomplete="off" name="j_password" value="" size="35">
</div>
<button type="button" class="pill cv-toggle">CV</button>
<div class="button-bar">
<button type="submit" class="button th-navigation ">LOG IN</button>
</div>
<div class="dynaLink parbase noLogin">
<div class="field nolabel">
<a href="/it/request-credentials">Hai dimenticato la password ?</a>
</div>
</div>
</fieldset>
<div class="end">
<div class="form_row">
<div class="form_leftcol"></div>
<div class="form_rightcol"></div>
</div>
<div class="form_row_description"></div>
</div>
这样做,我写的代码是:
<div class="form"> </div>
<fieldset class="inlined">
<input type="hidden" name="label" value="">
<input type="hidden" name="textName" value="j_username">
<div class="field required ">
<label for="_content_b2b_eu_login_jcr_content_par_start_j_username">Username</label>
<input type="text" class=" large" id="_content_b2b_eu_login_jcr_content_par_start_j_username" name="j_username" value="" placeholder="" onkeydown="">
</div>
<input type="hidden" name="password" value="j_password">
<div class="field required ">
<label for="_content_b2b_eu_login_jcr_content_par_start_j_password">Password</label>
<input class="large" id="_content_b2b_eu_login_jcr_content_par_start_j_password" type="password" autocomplete="off" name="j_password" value="" size="35">
</div>
<button type="button" class="pill cv-toggle">CV</button>
<div class="button-bar">
<button type="submit" class="button th-navigation ">LOG IN</button>
</div>
<div class="dynaLink parbase noLogin">
<div class="field nolabel">
<a href="/it/request-credentials">Hai dimenticato la password ?</a>
</div>
</div>
</fieldset>
<div class="end">
<div class="form_row">
<div class="form_leftcol"></div>
<div class="form_rightcol"></div>
</div>
<div class="form_row_description"></div>
</div>
$fields_array = array(
'formid' => urlencode('_content_b2b_eu_login_jcr_content_par_start'),
'formstart' => urlencode('/content/b2b/it/login/jcr:content/start'),
'_charset_' => urlencode('UTF-8'),
'redirect' => urlencode('/content/b2b/it/login/dashboard.html'),
'label' => urlencode(''),
'textName' => urlencode('j_username'),
'j_username' => urlencode('MYUSERNAME'),
'password' => urlencode('j_password'),
'j_password' => urlencode('MYPASSWORD')
);
$fields_string = '';
foreach($fields_array as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');
$url = 'https://my.shimano-eu.com/it/login.html';
$boundary = '7aBMjcE3CIYntqQ3';
$header = array('Content-Type: multipart/form-data;----WebKitFormBoundary'.$boundary);
curl_setopt($ch,CURLOPT_HTTPHEADER,$header);
curl_setopt($ch, CURLOPT_COOKIEFILE, $ckfile); //Uses cookies from the temp file
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
$output = curl_exec($ch);
之后,我看到它会自动发出POST请求“”,发布以下字段\u字符串:
<div class="form"> </div>
<fieldset class="inlined">
<input type="hidden" name="label" value="">
<input type="hidden" name="textName" value="j_username">
<div class="field required ">
<label for="_content_b2b_eu_login_jcr_content_par_start_j_username">Username</label>
<input type="text" class=" large" id="_content_b2b_eu_login_jcr_content_par_start_j_username" name="j_username" value="" placeholder="" onkeydown="">
</div>
<input type="hidden" name="password" value="j_password">
<div class="field required ">
<label for="_content_b2b_eu_login_jcr_content_par_start_j_password">Password</label>
<input class="large" id="_content_b2b_eu_login_jcr_content_par_start_j_password" type="password" autocomplete="off" name="j_password" value="" size="35">
</div>
<button type="button" class="pill cv-toggle">CV</button>
<div class="button-bar">
<button type="submit" class="button th-navigation ">LOG IN</button>
</div>
<div class="dynaLink parbase noLogin">
<div class="field nolabel">
<a href="/it/request-credentials">Hai dimenticato la password ?</a>
</div>
</div>
</fieldset>
<div class="end">
<div class="form_row">
<div class="form_leftcol"></div>
<div class="form_rightcol"></div>
</div>
<div class="form_row_description"></div>
</div>
j_username:MYUSERNAME
j_password:MYPASSWORD
j_validate:true
_charset_:UTF-8
我用下面的代码回答:
<div class="form"> </div>
<fieldset class="inlined">
<input type="hidden" name="label" value="">
<input type="hidden" name="textName" value="j_username">
<div class="field required ">
<label for="_content_b2b_eu_login_jcr_content_par_start_j_username">Username</label>
<input type="text" class=" large" id="_content_b2b_eu_login_jcr_content_par_start_j_username" name="j_username" value="" placeholder="" onkeydown="">
</div>
<input type="hidden" name="password" value="j_password">
<div class="field required ">
<label for="_content_b2b_eu_login_jcr_content_par_start_j_password">Password</label>
<input class="large" id="_content_b2b_eu_login_jcr_content_par_start_j_password" type="password" autocomplete="off" name="j_password" value="" size="35">
</div>
<button type="button" class="pill cv-toggle">CV</button>
<div class="button-bar">
<button type="submit" class="button th-navigation ">LOG IN</button>
</div>
<div class="dynaLink parbase noLogin">
<div class="field nolabel">
<a href="/it/request-credentials">Hai dimenticato la password ?</a>
</div>
</div>
</fieldset>
<div class="end">
<div class="form_row">
<div class="form_leftcol"></div>
<div class="form_rightcol"></div>
</div>
<div class="form_row_description"></div>
</div>
$url = 'https://my.shimano-eu.com/it/j_security_check';
$fields_string = 'j_username=MYUSERNAME&j_password=MYPASSWORD&j_validate=true&_charset_=UTF-8';
curl_setopt($ch, CURLOPT_COOKIEFILE, $ckfile); //Uses cookies from the temp file
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
$output = curl_exec($ch);
显然,首先,代码向登录页面发出GET请求,将cookie存储在$ckfile中
<div class="form"> </div>
<fieldset class="inlined">
<input type="hidden" name="label" value="">
<input type="hidden" name="textName" value="j_username">
<div class="field required ">
<label for="_content_b2b_eu_login_jcr_content_par_start_j_username">Username</label>
<input type="text" class=" large" id="_content_b2b_eu_login_jcr_content_par_start_j_username" name="j_username" value="" placeholder="" onkeydown="">
</div>
<input type="hidden" name="password" value="j_password">
<div class="field required ">
<label for="_content_b2b_eu_login_jcr_content_par_start_j_password">Password</label>
<input class="large" id="_content_b2b_eu_login_jcr_content_par_start_j_password" type="password" autocomplete="off" name="j_password" value="" size="35">
</div>
<button type="button" class="pill cv-toggle">CV</button>
<div class="button-bar">
<button type="submit" class="button th-navigation ">LOG IN</button>
</div>
<div class="dynaLink parbase noLogin">
<div class="field nolabel">
<a href="/it/request-credentials">Hai dimenticato la password ?</a>
</div>
</div>
</fieldset>
<div class="end">
<div class="form_row">
<div class="form_leftcol"></div>
<div class="form_rightcol"></div>
</div>
<div class="form_row_description"></div>
</div>
在所有这些之后,两个请求中的响应总是“禁止”的
<div class="form"> </div>
<fieldset class="inlined">
<input type="hidden" name="label" value="">
<input type="hidden" name="textName" value="j_username">
<div class="field required ">
<label for="_content_b2b_eu_login_jcr_content_par_start_j_username">Username</label>
<input type="text" class=" large" id="_content_b2b_eu_login_jcr_content_par_start_j_username" name="j_username" value="" placeholder="" onkeydown="">
</div>
<input type="hidden" name="password" value="j_password">
<div class="field required ">
<label for="_content_b2b_eu_login_jcr_content_par_start_j_password">Password</label>
<input class="large" id="_content_b2b_eu_login_jcr_content_par_start_j_password" type="password" autocomplete="off" name="j_password" value="" size="35">
</div>
<button type="button" class="pill cv-toggle">CV</button>
<div class="button-bar">
<button type="submit" class="button th-navigation ">LOG IN</button>
</div>
<div class="dynaLink parbase noLogin">
<div class="field nolabel">
<a href="/it/request-credentials">Hai dimenticato la password ?</a>
</div>
</div>
</fieldset>
<div class="end">
<div class="form_row">
<div class="form_leftcol"></div>
<div class="form_rightcol"></div>
</div>
<div class="form_row_description"></div>
</div>
我在这里三天了,我错在哪里了
<div class="form"> </div>
<fieldset class="inlined">
<input type="hidden" name="label" value="">
<input type="hidden" name="textName" value="j_username">
<div class="field required ">
<label for="_content_b2b_eu_login_jcr_content_par_start_j_username">Username</label>
<input type="text" class=" large" id="_content_b2b_eu_login_jcr_content_par_start_j_username" name="j_username" value="" placeholder="" onkeydown="">
</div>
<input type="hidden" name="password" value="j_password">
<div class="field required ">
<label for="_content_b2b_eu_login_jcr_content_par_start_j_password">Password</label>
<input class="large" id="_content_b2b_eu_login_jcr_content_par_start_j_password" type="password" autocomplete="off" name="j_password" value="" size="35">
</div>
<button type="button" class="pill cv-toggle">CV</button>
<div class="button-bar">
<button type="submit" class="button th-navigation ">LOG IN</button>
</div>
<div class="dynaLink parbase noLogin">
<div class="field nolabel">
<a href="/it/request-credentials">Hai dimenticato la password ?</a>
</div>
</div>
</fieldset>
<div class="end">
<div class="form_row">
<div class="form_leftcol"></div>
<div class="form_rightcol"></div>
</div>
<div class="form_row_description"></div>
</div>
这是我尝试登录的页面:。侧注:您不必将查询字符串传递给
CURLOPT_POSTFIELDS
,也可以传递包含字段的数组。正如@CharlotteDunois所指出的,您可以将参数数组传递给curl请求-这有效地更改了enctype
(内容类型)从application/x-www-form-urlencoded
到“multipart/formdata”-这就是边界发挥作用的地方感谢你们的回答,我在中更改了Curl请求:Curl_setopt($ch,CURLOPT_HTTPHEADER,$header);curl_setopt($ch,CURLOPT_COOKIEFILE,$ckfile);curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);curl_setopt($ch,CURLOPT_URL,$URL);curl_setopt($ch,CURLOPT_POST,count($fields_array));curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_数组);curl_setopt($ch,CURLINFO_HEADER_OUT,true)$输出=curl_exec($ch);但不幸的是,响应il也是“禁止的”。旁注:您不必将查询字符串传递给CURLOPT_POSTFIELDS
,您也可以传递包含字段的数组。正如@CharlotteDunois所指出的,您可以向curl请求传递一个参数数组-这有效地更改了enctype
(内容类型)从application/x-www-form-urlencoded
到“multipart/formdata”-这就是边界发挥作用的地方感谢你们的回答,我在中更改了Curl请求:Curl_setopt($ch,CURLOPT_HTTPHEADER,$header);curl_setopt($ch,CURLOPT_COOKIEFILE,$ckfile);curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);curl_setopt($ch,CURLOPT_URL,$URL);curl_setopt($ch,CURLOPT_POST,count($fields_array));curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_数组);curl_setopt($ch,CURLINFO_HEADER_OUT,true)$输出=curl_exec($ch);但不幸的是,这种反应也被“禁止”。
<div class="form"> </div>
<fieldset class="inlined">
<input type="hidden" name="label" value="">
<input type="hidden" name="textName" value="j_username">
<div class="field required ">
<label for="_content_b2b_eu_login_jcr_content_par_start_j_username">Username</label>
<input type="text" class=" large" id="_content_b2b_eu_login_jcr_content_par_start_j_username" name="j_username" value="" placeholder="" onkeydown="">
</div>
<input type="hidden" name="password" value="j_password">
<div class="field required ">
<label for="_content_b2b_eu_login_jcr_content_par_start_j_password">Password</label>
<input class="large" id="_content_b2b_eu_login_jcr_content_par_start_j_password" type="password" autocomplete="off" name="j_password" value="" size="35">
</div>
<button type="button" class="pill cv-toggle">CV</button>
<div class="button-bar">
<button type="submit" class="button th-navigation ">LOG IN</button>
</div>
<div class="dynaLink parbase noLogin">
<div class="field nolabel">
<a href="/it/request-credentials">Hai dimenticato la password ?</a>
</div>
</div>
</fieldset>
<div class="end">
<div class="form_row">
<div class="form_leftcol"></div>
<div class="form_rightcol"></div>
</div>
<div class="form_row_description"></div>
</div>