使用curl从Homestead来宾VM内访问主机

发布于 01-10 04:21 字数 890 浏览 4 评论 0原文

我正在尝试使用curl 从 Homestead VM 中访问在我的主机上运行的 api url。当我从来宾虚拟机中执行代码时,它返回错误 无法连接到 10.0.2.15 端口 8001:连接被拒绝10.0.2.15 是主机的 IP客人里面。我怀疑必须在端口转发中找到解决方案,我已尝试在 Homestead.yml 内进行端口转发,但它没有解决问题。

这是卷曲调用。

$defaultParams = array(
    CURLOPT_HEADER => TRUE,
    CURLOPT_RETURNTRANSFER => TRUE,
    CURLINFO_HEADER_OUT => TRUE,
    CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
    CURLOPT_USERPWD => $this->apiKey.':',
    CURLOPT_HTTPHEADER => array( 'Expect:' ),
    CURLOPT_FAILONERROR => true
);

$url = 'http://10.0.2.15:8001/api/languages?display=full&ws_key=xxxxx';

$session = curl_init($url);

...

如果我访问主机上的网址,我会得到预期的结果:

在此处输入图像描述

I'm trying to access api url which runs on my host machine from within a Homestead VM with curl. When I execute the code from within the guest VM it returns the error Failed to connect to 10.0.2.15 port 8001: Connection refused, 10.0.2.15 is the ip of the host inside the guest. I suspect the solution must be found in port forwading, I have tried port forwarding inside Homestead.yml but it did't resolve the issue.

This is the curl call.

$defaultParams = array(
    CURLOPT_HEADER => TRUE,
    CURLOPT_RETURNTRANSFER => TRUE,
    CURLINFO_HEADER_OUT => TRUE,
    CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
    CURLOPT_USERPWD => $this->apiKey.':',
    CURLOPT_HTTPHEADER => array( 'Expect:' ),
    CURLOPT_FAILONERROR => true
);

$url = 'http://10.0.2.15:8001/api/languages?display=full&ws_key=xxxxx';

$session = curl_init($url);

...

If I access the url on the host I get the expected result:

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文