如何使用 Jquery 或 PHP 从其他网站获得响应?
How to get a Response from other web site, using Jquery or PHP?
我有一个问题
我需要制作一个登录表单,我必须从其他网站获得响应,我知道有 HttpResponseMessage,但我认为这是为了消耗网络 API,所以我真的很想知道如何获得网站的响应,因为我需要成功登录或者登录失败
有什么方法可以做到吗?
和代码:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#btnSub").click(function(){
if($("#User").val() == "" || $("#Pass").val() == ""){
alert("Insert User & Password");
event.preventDefault();
}else{
$("#LogBox").hide();
}
});
});
</script>
<section class="container">
<div class="login">
<h1>Inicio de Sesión</h1>
<form action="http://us.mzoneweb.net/Scope.MZone.Web/Logon.ashx" method="post" target="TheMZoneFrame">
<p><input name="Username" id="User" type="text" placeholder="Usuario"></p>
<p><input name="Password" id="Pass" type="password" placeholder="Contraseña"></p>
<p><input name="LogoutUrl" type="hidden" value=""></p>
<p><input name="ErrorUrl" type="hidden" value=""></p>
<p><input name="Version" type="hidden" value=""></p>
<p class="remember_me">
<label>
<input type="checkbox" name="remember_me" id="remember_me">
Remember me on this computer
</label>
</p>
<p class="submit"><input name="Submit1" id="btnSub" type="submit" value="Iniciar Sesión" ></p>
</form>
</div>
</section>
<iframe src="" height="100%" id="TheMZoneFrame" name="TheMZoneFrame" width="100%" frameborder="0">
<noframes>Your browser does not support IFRAME elements.</noframes>
</iframe>
使用cURL.
这是一个示例函数,它将从另一个站点获得响应:
/*
* Makes an HTTP request via GET or POST, and can download a file
* @returns - Returns the response of the request
* @param $url - The URL to request, including any GET parameters
* @param $params - An array of POST values to send
* @param $filename - If provided, the response will be saved to the
* specified filename
*/
function request($url, $params = array(), $filename = "") {
$ch = curl_init();
$curlOpts = array(
CURLOPT_URL => $url,
// Set Useragent
CURLOPT_USERAGENT =>
'Mozilla/5.0 (Windows NT 6.3; WOW64; rv:29.0)
Gecko/20100101 Firefox/29.0',
// Don't validate SSL
// This is to prevent possible errors with self-signed certs
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true
);
if(!empty($filename)){
// If $filename exists, save content to file
$file2 = fopen($filename,'w+')
or die("Error[".__FILE__.":".__LINE__."]
Could not open file: $filename");
$curlOpts[CURLOPT_FILE] = $file2;
}
if (!empty($params)) {
// If POST values are given, send that shit too
$curlOpts[CURLOPT_POST] = true;
$curlOpts[CURLOPT_POSTFIELDS] = $params;
}
curl_setopt_array($ch, $curlOpts);
$answer = curl_exec($ch);
// If there was an error, show it
if (curl_error($ch)) die(curl_error($ch));
if(!empty($filename)) fclose($file2);
curl_close($ch);
return $answer;
}
我有一个问题
我需要制作一个登录表单,我必须从其他网站获得响应,我知道有 HttpResponseMessage,但我认为这是为了消耗网络 API,所以我真的很想知道如何获得网站的响应,因为我需要成功登录或者登录失败
有什么方法可以做到吗?
和代码:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#btnSub").click(function(){
if($("#User").val() == "" || $("#Pass").val() == ""){
alert("Insert User & Password");
event.preventDefault();
}else{
$("#LogBox").hide();
}
});
});
</script>
<section class="container">
<div class="login">
<h1>Inicio de Sesión</h1>
<form action="http://us.mzoneweb.net/Scope.MZone.Web/Logon.ashx" method="post" target="TheMZoneFrame">
<p><input name="Username" id="User" type="text" placeholder="Usuario"></p>
<p><input name="Password" id="Pass" type="password" placeholder="Contraseña"></p>
<p><input name="LogoutUrl" type="hidden" value=""></p>
<p><input name="ErrorUrl" type="hidden" value=""></p>
<p><input name="Version" type="hidden" value=""></p>
<p class="remember_me">
<label>
<input type="checkbox" name="remember_me" id="remember_me">
Remember me on this computer
</label>
</p>
<p class="submit"><input name="Submit1" id="btnSub" type="submit" value="Iniciar Sesión" ></p>
</form>
</div>
</section>
<iframe src="" height="100%" id="TheMZoneFrame" name="TheMZoneFrame" width="100%" frameborder="0">
<noframes>Your browser does not support IFRAME elements.</noframes>
</iframe>
使用cURL.
这是一个示例函数,它将从另一个站点获得响应:
/*
* Makes an HTTP request via GET or POST, and can download a file
* @returns - Returns the response of the request
* @param $url - The URL to request, including any GET parameters
* @param $params - An array of POST values to send
* @param $filename - If provided, the response will be saved to the
* specified filename
*/
function request($url, $params = array(), $filename = "") {
$ch = curl_init();
$curlOpts = array(
CURLOPT_URL => $url,
// Set Useragent
CURLOPT_USERAGENT =>
'Mozilla/5.0 (Windows NT 6.3; WOW64; rv:29.0)
Gecko/20100101 Firefox/29.0',
// Don't validate SSL
// This is to prevent possible errors with self-signed certs
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true
);
if(!empty($filename)){
// If $filename exists, save content to file
$file2 = fopen($filename,'w+')
or die("Error[".__FILE__.":".__LINE__."]
Could not open file: $filename");
$curlOpts[CURLOPT_FILE] = $file2;
}
if (!empty($params)) {
// If POST values are given, send that shit too
$curlOpts[CURLOPT_POST] = true;
$curlOpts[CURLOPT_POSTFIELDS] = $params;
}
curl_setopt_array($ch, $curlOpts);
$answer = curl_exec($ch);
// If there was an error, show it
if (curl_error($ch)) die(curl_error($ch));
if(!empty($filename)) fclose($file2);
curl_close($ch);
return $answer;
}