cURL本地dev网络问题与虚拟主机命名 [英] cURL on local dev network problem with virtual host naming

查看:191
本文介绍了cURL本地dev网络问题与虚拟主机命名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试在虚拟服务器上进行一些本地测试,问题是cURL返回一个http_code => 0

Trying to do some local testing on a virtual server, the problem is cURL is returning a http_code => 0

我认为这是与我的虚拟主机命名。

I think it's to do with my virtual host naming.

虚拟主机名称:dev.project

Virtual Host Name: dev.project

cURL请求正在添加http://

the cURL request is adding http://

如果我ping:dev.project从命令行,我得到一个命中。
如果我尝试使用 http://dev.project 我会得到未知的主机。

if I ping: dev.project from the command line, I get a hit. If I try it with http://dev.project I get unknown host.

有没有curl_setopt选项只是为了使用主机名?我不知道如果我可以使用IP,因为服务器上有几个项目,或者Apache会处理这个吗?

Is there a curl_setopt option just to use the hostname? I'm no sure if I can use the IP as there are several projects on the server, or would Apache handle this?

这是我试过的: p>

Here is what I have tried:

$request_url  = 'dev.project';
$request_args = 'parm=1234';
$user_agent   = 'Mozilla/5.0 (X11; U; Linux i686; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.648.151 Safari/534.16';

$ch = curl_init();

// set curl options (GET)       
curl_setopt($ch, CURLOPT_URL, $request_url.'?'.$request_args);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
//curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
//curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
//curl_setopt($ch, CURLOPT_FAILONERROR, TRUE);
//curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
//curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
//curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // CHANGE THIS TO TRUE

// Set curl options (POST)
//curl_setopt($ch, CURLOPT_URL, $request_url);
//curl_setopt($ch, CURLOPT_POST, TRUE);
//curl_setopt($ch, CURLOPT_POSTFIELDS, $request_args);
//curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
//curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
//curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
//curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
//curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

$response = curl_exec($ch); 
echo "<pre>".print_r($response,true)."</pre><br />\n"; // nothing is returned
print_r(curl_getinfo($ch));

curl_close($ch); 

来自curl_getinfo()的响应:(注意:http://已预先在URL中)

Response from curl_getinfo() : (NOTE: the http:// is pre-pended in the url)

Array
(
    [url] => http://dev.project?parm=1234
    [http_code] => 0
    [header_size] => 0
    [request_size] => 0
    [filetime] => -1
    [ssl_verify_result] => 0
    [redirect_count] => 0
    [total_time] => 0
    [namelookup_time] => 0
    [connect_time] => 0
    [pretransfer_time] => 0
    [size_upload] => 0
    [size_download] => 0
    [speed_download] => 0
    [speed_upload] => 0
    [download_content_length] => 0
    [upload_content_length] => 0
    [starttransfer_time] => 0
    [redirect_time] => 0
)


推荐答案

,这里是修复:

// This is your Virtual Hosts name
$request_host   = 'dev.project'; 

// This is the IP
$request_url    = '192.168.0.1';

$headers = array("Host: ".$request_host);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);     
curl_setopt($ch, CURLOPT_URL, $request_url.'?'.$request_args);
curl_setopt($ch, CURLOPT_HEADER, FALSE);

这篇关于cURL本地dev网络问题与虚拟主机命名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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