在PHP中是否可以使用curl和相对路径? [英] Is it possible to use curl with relative path in PHP?

查看:647
本文介绍了在PHP中是否可以使用curl和相对路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个php页面。我要在a.php中获取b.php。

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

在我的a.php中:

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

无效;

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

可以。我确定a.php在www.site.com下。

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

为什么curl不能使用相对路径?是否有解决方法?

Why curl can't work with relative path? Is there a workaround?

推荐答案

Curl是一个独立的库,它不真正知道网络服务器的来源,哲学)为什么它在那里。因此,您可以使用两个_SERVER变量之一来伪造相对网址:

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']




Host的内容:当前请求中的标题,如果有的话。

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

请参阅: http://php.net/manual/en/reserved.variables.server.php

编辑更新:
我想了一会儿,你真的需要用curl抓取它吗?
你通常也可以获取这样的另一个脚本的任何输出,并节省通过一个新的http请求加载它的开销:

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

这篇关于在PHP中是否可以使用curl和相对路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆