Using PHP as a server forwarding layer to solve JS's Ajax cross-domain access problem _php Tutorial

Source: Internet
Author: User
When you do the JavaScript AJAX application, you will encounter the interface you need to request is not in your current domain, there will be cross-domain access issues, the browser will prohibit you to request this interface.

How to access this WebService interface at this time?

An easy way is to add a forwarding layer on the server of this domain, after receiving the request from the browser, forward the request to the corresponding WebService by the server, and then return the result back to the JS request page.

This is generally the safest and most compatible way to solve cross-domain access.

Here is a PHP script I wrote, can complete this forwarding process, for reference only:

[PHP]
/**
* Interface forwarding layer in AJAX service processing to solve the problem of Ajax cross-domain access
* Working principle: Ask the request through this program to do the relay, at the local server layer to complete the interaction with the remote service interface
* Note: When using url_root this parameter needs to be modified according to your target interface address, this forwarding layer can be used for single interface Web service interface Services
* The program supports simultaneous forwarding of post data and get quantity;
* @version 1.0.0.2
* @author Jerryli Lijian@dzs.mobi
* @copyright B.dzs.mobi 2012-11-16
* */
Class Interface_relay
{
/** interface Root address (here is where you need to modify) */
Const URL_ROOT = ' http://api.air-id.net/InterFace/';
/** Character Set */
Const CHARSET = ' UTF-8 ';
/**get*/
Private $msGets = ';
/**post*/
Private $maGetPostData = Array ();

function __construct ()
{
$this->getpost ();
$this->getget ();
if ($this->msgets! = "| | count ($this->magetpostdata) > 0)
{//presence of input data
if (count ($this->msgets) > 0)
$SURL = Self::url_root. '? '. $this->msgets;
Else
$SURL = Self::url_root;
Header (' content-type:text/html; charset= '. Self::charset);
echo $this->getcontent ($SURL);
}
Else
{
Header (' content-type:text/html; charset= '. Self::charset);
echo $this->getcontent (self::url_root);
}
}

function __destruct ()
{
Unset ($maGetPostData, $msGets);
}

/**
* Load Post data
* @return BOOL
* */
Private Function Getpost ()
{
$handle = @fopen (' php://input ', ' R ');
$data = ";
Do
{
$data = @fread ($handle, 1024);
if (strlen ($data) = = 0)
Break
Else
$this->magetpostdata[] = $data;
}while (TRUE);
Fclose ($handle);
Unset ($data, $handle);
Return count ($this->magetpostdata) >= 1;
}

/**
* Loading Get data
* @return BOOL
* */
Private Function Getget ()
{www.2cto.com
/* Obtain get content */
if (count ($_get) > 0)
{
$ATMP = Array ();
foreach ($_get as $sKey = $sVal)
$ATMP [] = $sKey. ' ='. UrlEncode ($sVal);
$this->msgets = Implode (' & ', $ATMP);
return true;
}
Else
return false;
}

/**
* Read the contents returned by the remote interface
* @return String
* */
Private Function GetContent ($SGETURL)
{
/**/
$ch = Curl_init ();
curl_setopt ($ch, Curlopt_url, $SGETURL); Set the URL address of the Get
curl_setopt ($ch, Curlopt_returntransfer, true);//Save the result as a string
curl_setopt ($ch, Curlopt_connecttimeout, 10);//connection Timeout time s
curl_setopt ($ch, Curlopt_timeout, 10);//Execution Timeout time s
curl_setopt ($ch, Curlopt_dns_cache_timeout, 1800);//dns parsing cache saved for half an hour
curl_setopt ($ch, curlopt_header,0);//Lose header information
if (count ($this->magetpostdata) > 0)
{//There is post data to be submitted
curl_setopt ($ch, Curlopt_post, 1); Enable post data
curl_setopt ($ch, Curlopt_postfields, Implode (", $this->magetpostdata));//Submit post data
}
$sData = curl_exec ($ch);
Curl_close ($ch);
Unset ($ch);
return $sData;
}
}

$o = new Interface_relay ();
Unset ($o);
?>

http://www.bkjia.com/PHPjc/477904.html www.bkjia.com true http://www.bkjia.com/PHPjc/477904.html techarticle When you do the JavaScript AJAX application, you will encounter the interface you need to request is not in your current domain, there will be cross-domain access issues, the browser will prohibit you to request this interface. How to visit at this time ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.