2

我有 2 个 php 文件:“1.php”和“2.php”。

“1.php”的内容如下:

<?PHP
$url = "http://www.google.com/";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$html = curl_exec($ch);
echo $html;
curl_close($ch);
?>

当我去http://domain.com/1.php -> 它将返回 www.google.com 的内容

问题是:
如何使用“2.php”获取“1.php”的“www.google.com”网址?

(我想当我去“ http://domain.com/2.php ”时它会显示“www.google.com”文本。)

4

1 回答 1

1

Instead of having two files, you can use three: a configuration file, which will contain the URL, and two others that will use this URL in two different ways.

For example (to use your filenames):

conf.php (configutation file):

<?php
$url = "http://www.google.fr/";

1.php (your code):

<?php
//Include conf file
require 'conf.php';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$html = curl_exec($ch);
echo $html;
curl_close($ch);

2.php (your code again):

<?php
//Include conf file
require 'conf.php';

echo $url;

Some possible improvements: use less generic names and an array (instead $url we can consider for example $conf['url'])

于 2014-02-04T06:44:22.680 回答