如何使用Jquery或PHP从其他网站获取响应?

时间:2015-09-15 16:16:51

标签: php jquery html http-response-codes

我有一个查询

我需要创建一个登录表单,我必须从其他网站获得响应,我知道有HttpResponseMessage,但我认为这是使用Web 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>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

使用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;
}