2

I have 2 php files: "1.php" & "2.php".

"1.php" with content like this:

<?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);
?>

when I go to http://domain.com/1.php -> it will return content of www.google.com

The question is:
How can I get the URL which is the "www.google.com" of "1.php" using "2.php"?

(I want when I go to "http://domain.com/2.php" it would show "www.google.com" text.)

1 Answer 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'])

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.