gpt4 book ai didi

php - cURL 关于虚拟主机命名的本地开发网络问题

转载 作者:可可西里 更新时间:2023-11-01 00:17:04 25 4
gpt4 key购买 nike

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

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

虚拟主机名:dev.project

cURL 请求正在添加 http://

如果我从命令行 ping: dev.project,就会成功。如果我用 http://dev.project 试试我得到了未知的主机。

是否有一个 curl_setopt 选项只是为了使用主机名?我不确定我是否可以使用该 IP,因为服务器上有多个项目,或者 Apache 会处理这个吗?

这是我尝试过的:

$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 中预先设置)

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

关于php - cURL 关于虚拟主机命名的本地开发网络问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5437177/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com