是否可以在 PHP 中使用带有相对路径的 curl?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6615971/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible to use curl with relative path in PHP?
提问by Lai Yu-Hsuan
I have two php pages. I want to fetch b.php in a.php.
我有两个 php 页面。我想在 a.php 中获取 b.php。
In my a.php:
在我的 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.
没问题。我确定 a.php 在 www.site.com 下。
Why curl can't work with relative path? Is there a workaround?
为什么 curl 不能使用相对路径?有解决方法吗?
回答by Philip Ramirez
How about taking the domain from HTTP_HOST?
从 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);
回答by Arend
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:
Curl 是一个单独的库,它并不真正了解网络服务器及其来源或(哲学上)为什么存在。因此,您可以使用两个 _SERVER 变量之一来“伪造”相对网址:
$_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.
Host 的内容:来自当前请求的标头,如果有的话。
See: http://php.net/manual/en/reserved.variables.server.php
请参阅: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:
编辑更新:我想了一会儿:你真的需要用 curl 来获取它吗?您通常也可以像这样获取另一个脚本的任何输出,并节省通过新的 http 请求加载它的开销:
ob_start();
require "b.php";
$output = ob_get_clean();
回答by hakre
cUrl needs an absolute URI to operate on.
cUrl 需要一个绝对 URI 来操作。
A relative URI does not work because there is no base URI given to which that relative URI is absolute to.
相对 URI 不起作用,因为没有给出相对 URI 是绝对的基础 URI。
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.
但是,如果您同时拥有基本 URI 和相对 URI,则可以创建相对 URI 的绝对 URI 并将其与 cUrl 一起使用。
See 12.4.1 Resolving relative URIs.
请参阅12.4.1 解析相对 URI。
A PHP class that can build an absolute URI based on a relative URI and it's base is the Net_URL2package in Pear.
一个可以基于相对 URI 构建绝对 URI 的 PHP 类,它的基础是Pear 中的Net_URL2包。
回答by Hyman Murdoch
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.
cURL 主要用于从外部域检索数据,因此允许相对路径没有太大意义。最简单的方法就是将您当前的域附加到 URL。
$domain = $_SERVER['HTTP_HOST'] . "/";
$ch = curl_init($domain . "b.php");
echo(curl_exec($ch));
curl_close($ch);
回答by Marc B
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
?
CURL 完全不知道它的运行环境。它无法知道 'b.php' 在哪里。应该把它变成example.org/b.php
还是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?
即使将其视为本地文件引用也无济于事...... CURL 不会知道.php
文件实际上是一个 PHP 脚本。即使它执行了本地文件提取,您也只会获得 PHP 源代码,而不是 PHP 运行后的脚本输出。如果您有一个网站arstechnica.com
,其所有页面实际上都是.ars
脚本,该怎么办?那是.asp吗?.aspx?.html?PHP脚本?珀尔?红宝石?
So.. simple answer: you must always specify a complete URL, with protocol, for CURL to operate on.
所以.. 简单的答案:您必须始终指定一个完整的 URL 和协议,以便 CURL 进行操作。