Use PHP as the server forwarding layer to solve the ajax cross-domain access problem of js_PHP tutorial

WBOY
Release: 2016-07-14 10:06:54
Original
1148 people have browsed it

When making a js ajax application, you will encounter that the interface you need to request is not in your current domain. At this time, cross-domain access problems will occur, and the browser will prohibit you from requesting this interface.

How to access the interface of this WebService at this time?

A simple 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 through the server, and then retrieve the returned result. , send back the js request page.

Generally speaking, this is the safest and most compatible way to solve cross-domain access.

The following is a php script I wrote to complete this forwarding process, for reference only:

[php]
/**
* The interface forwarding layer in ajax business processing solves the problem of ajax cross-domain access
* Working principle: The request is transferred through this program, and the interaction with the remote service interface is completed at the local server layer
* Note: When using, the URL_ROOT 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 (this is what needs to be modified)*/ 
    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->get
* Load POST data
* @return bool
*(); 
        $this->get
* Load GET data
* @return bool
*(); 
        if($this->msGets != '' || count($this->maGetPostData) > 0) 
        {   //存在输入数据 
            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); 
    } 
 
    /**
     * 载入数据
     * @return bool
     * */ 
    private function get() 
    { 
        $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; 
    } 
      /**      * 载入数据      * @return bool      * */      private function get() { www.2cto.com
/*Get 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 content 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 GET
​​​​ curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);//Save the result as a string
​​​​ curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 10);//Connection timeout s
         curl_setopt ($ch, CURLOPT_TIMEOUT, 10);//Execution timeout time s
​​​​ curl_setopt ($ch, CURLOPT_DNS_CACHE_TIMEOUT, 1800);//DNS resolution cache storage time is half an hour
         curl_setopt($ch, CURLOPT_HEADER,0);//Discard header information
If (count($this->maGetPostData) > 0)
{ //There is POST data that needs 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.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/477904.htmlTechArticleWhen doing js ajax application, you will encounter that the interface you need to request is not in your current domain. At this time There will be cross-domain access problems, and the browser will prohibit you from requesting this interface. How to visit at this time...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template