开发者

Is it possible to use curl with relative path in PHP?

开发者 https://www.devze.com 2023-03-18 22:11 出处:网络
I have two php pages. I want to fetch b.php in a.php. In my a.php: $ch = curl_init(\"b.php\"); echo(curl_exec($ch));

I have two php pages. I want to fetch b.php in a.php.

In my a.php:

$ch = curl_init("b.php");
echo(curl_exec($ch));
curl_close($ch);

Doesn't work;

But:

$ch = curl_init("www.site.com/b.php");
echo(curl_exec($ch));
curl_close($ch);

is OK. I'm sure a.php is under www.site.com.

Why curl can't work with relative path? Is there a开发者_JS百科 workaround?


Curl is a seperate library which does not really know anything about webservers and where it's coming from or (philosophicaly) why it is there. So you may 'fake' relative urls using one of the two _SERVER variables:

$_SERVER['SERVER_NAME'] 

The name of the server host under which the current script is executing. If the script is running on a virtual host, this will be the value defined for that virtual host.

$_SERVER['HTTP_HOST']

Contents of the Host: header from the current request, if there is one.

See: http://php.net/manual/en/reserved.variables.server.php

Edit update: I thought a moment longer about this: do you really need to fetch it with curl? You usually may also fetch any output of another script like this and save the overhead of loading it through a new http request:

ob_start();
require "b.php";
$output = ob_get_clean();


How about taking the domain from HTTP_HOST?

$domain = $_SERVER['HTTP_HOST'];
$prefix = $_SERVER['HTTPS'] ? 'https://' : 'http://';
$relative = '/b.php';
$ch = curl_init($prefix.$domain.$relative);
echo(curl_exec($ch));
curl_close($ch);


cUrl needs an absolute URI to operate on.

A relative URI does not work because there is no base URI given to which that relative URI is absolute to.

You can however, if you have both the base URI and the relative URI, create the absolute URI of the relative URI and use it with cUrl.

See 12.4.1 Resolving relative URIs.

A PHP class that can build an absolute URI based on a relative URI and it's base is the Net_URL2 package in Pear.


cURL would primarily be used to retrieve data from external domains, therefore it wouldn't make too much sense to allow relative paths. The easiest thing to do would just be to append your current domain to the URL.

$domain = $_SERVER['HTTP_HOST'] . "/";
$ch = curl_init($domain . "b.php");
echo(curl_exec($ch));
curl_close($ch);


CURL has absolutely no knowledge of its operating environment. There is no way for it to know where 'b.php' is. Should it turn that into example.org/b.php or some.wonky.multi.level.domain.co.uk/b.php?

Even treating it as a local file reference would be useless... CURL wouldn't know that a .php file is actually a PHP script. Even if it did a local file fetch, you'd just get PHP source, not the output of the script after PHP's run it. What if you've got a site like arstechnica.com where all its pages are actually .ars scripts? Is that .asp? .aspx? .html? PHP script? perl? ruby?

So.. simple answer: you must always specify a complete URL, with protocol, for CURL to operate on.

0

精彩评论

暂无评论...
验证码 换一张
取 消