【问题标题】:Could not resolve host in Curl无法解析 Curl 中的主机
【发布时间】:2015-11-25 20:01:41
【问题描述】:

我正在与 Curl 合作向 php 脚本提出一些请愿书,我正在尝试如下所示提出请愿书,我的脚本是 ajax2.php

$params=['name'=>'John', 'surname'=>'Doe', 'age'=>36,'method'=>'prueba'];
$defaults = array(
    CURLOPT_URL => getcwd().'\src\myApp\ajax2.php',
    CURLOPT_POST => true,
    CURLOPT_POSTFIELDS => http_build_query($params),
);
$ch = curl_init();
curl_setopt_array($ch, ($options + $defaults));
curl_exec($ch);

if (curl_errno($ch)) {
    // this would be your first hint that something went wrong
    die('Couldn\'t send request: ' . curl_error($ch));

}

但我收到此错误:Couldn't send request: Could not resolve host: C 那么,我应该如何调用项目文件夹中的脚本?

【问题讨论】:

  • 您应该使用带有 http 或 https 的 URL。如果您在本地主机中,则 CURLOPT_URL => 'http://localhost/YOUT_APP_DIR/src/myApp/ajax2.php',
  • @jibon57 谢谢老兄!成功了!

标签: php curl


【解决方案1】:

curl 或 libcurl,正如他们在 official site 上所说的那样,是一个“URL 传输库”,即它期望在 URL 目标上工作。但是,您传递给它的文件路径类似于 C:\PathToYourStuff\src\myApp\ajax2.php,这不是有效的 URL 格式。这就是错误消息显示的原因

无法解析主机:C

将上面的路径解释为 URL 意味着 C 是主机名,因为冒号 (":") 是将主机名与 URL 中的端口分开的部分。 (从 URL 解析器的角度来看,这背后的部分是无稽之谈,但它甚至没有达到那么远,因为无法解析假定的主机名。)

因此,您必须改用指向该文件的 URL,例如类似于http://localhost/path-to-your-stuff/src/myApp/ajax2.php

因此,将您的代码更改为类似的内容并根据需要调整 URL:

$params=['name'=>'John', 'surname'=>'Doe', 'age'=>36,'method'=>'prueba'];
$defaults = array(
    CURLOPT_URL => 'http://localhost/path-to-your-stuff/src/myApp/ajax2.php',
    CURLOPT_POST => true,
    CURLOPT_POSTFIELDS => http_build_query($params),
);
$ch = curl_init();
curl_setopt_array($ch, ($options + $defaults));
curl_exec($ch);
// ... and so on, as seen in your question

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-23
    • 2011-05-20
    相关资源
    最近更新 更多