Home  >  Article  >  Backend Development  >  How to import files across domains in php

How to import files across domains in php

PHP中文网
PHP中文网Original
2017-07-29 18:19:071648browse

We need to understand a concept first, what is cross-domain? For example, when the a.js file under a.com calls the b.js file in b.com, cross-domain behavior occurs. Browsers will restrict this behavior due to the protection of the same-origin policy. Of course, there are solutions. You can use search engines to search. The PHP we are talking about today is a server-side language. It is different from the browser and belongs to the back-end language. How is its cross-domain reference file implemented? This article provides some methods, hoping to be helpful to students in need.

First of all, we assume that the background language of these two hosts is php. You can choose two online environments, or one online and one local. environment, or use a virtual machine (this is not our focus today, please solve it yourself). Create a.php and b.php in two environments respectively. The codes are as follows:

a.php:

<?php
echo &#39;我是老A,呼叫老B,听到请回答:<br>&#39;;
/* 以下是源服务器的代码*/
/* 以上是源服务器的代码*/
?>

b.php:

<?php
header("Content-type:text/html;charset=utf-8");
echo "我是老B,叫我干嘛????";?>

Use a browser to access a.php and b.php respectively to view the effect before setting:

Method 1: Open a. php, enter the following code:

<?php
echo &#39;我是老a,呼叫老b,请回答:<br>&#39;;
/* 以下是源服务器的代码*/
$file_path = "此处输入b.php的访问地址";
$str = file_get_contents($file_path);
$str = str_replace("\r\n","<br />",$str);
echo $str;
/* 以上是源服务器的代码*/
?>

Revisit a.php and check the effect:

Method 2: Modify a.php and paste the following Code:

<?php
echo &#39;我是老a,呼叫老b,请回答:<br>&#39;;
/* 以下是源服务器的代码*/
$file_path = "此处输入b.php的访问地址";
$fp = fopen($file_path,"r");
$str = "";
$buffer = 1024;
    while(!feof($fp)){
          $str .= fread($fp,$buffer);
     }
$str = str_replace("\r\n","<br />",$str);
echo $str;
/* 以上是源服务器的代码*/
?>

Revisit a.php and check the effect:

##Method 3: Modify a.php and paste the following code:

<?php
echo &#39;我是老a,呼叫老b,请回答:<br>&#39;;
/* 以下是源服务器的代码*/
$file_path = "此处输入b.php的访问地址";
$file_arr = file($file_path);
for($i=0;$i<count($file_arr);$i++){//逐行读取文件内容
echo $file_arr[$i]."<br />";
}
/* 以上是源服务器的代码*/
?>

Method 4: Modify a.php and adjust the following code:

<?php
echo &#39;我是老a,呼叫老b,请回答:<br>&#39;;
/* 以下是源服务器的代码*/
$url = "此处输入b.php的访问地址";
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout)
$contents = curl_exec($ch);
curl_close($ch);
echo $contents;
/* 以上是源服务器的代码*/
?>

Note: When using curl, please make sure that PHP has the curl module enabled


The above is the detailed content of How to import files across domains in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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