【问题标题】:Curl Post with php failing on localhost always带有 php 的 Curl Post 总是在 localhost 上失败
【发布时间】:2014-09-21 11:58:36
【问题描述】:

我正在使用 wamp 在我的本地机器上运行带有 php 的 Apache。我确保 dll 文件在 php.ini 中未注释(在 apache 和 php bin 文件夹下)。我有以下函数,每当我调用我都会收到错误:

Curl 失败,错误 7 无法连接到 127.0.0.1 端口 8000:连接被拒绝

函数是:

function post($data) {
    try{
    $postUrl = "127.0.0.1:8000";
    $ch = curl_init();
    curl_setopt($ch,CURLOPT_URL,$postUrl);
    curl_setopt($ch,CURLOPT_POST,TRUE);
    curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    $x = curl_exec($ch);

    if (FALSE === $x)
    throw new Exception(curl_error($ch), curl_errno($ch));

    curl_close($ch);

} catch (Exception $e){
    $x = 'Curl failed with error ' . $e->getCode().' '.$e->getMessage();
}
return $x;
}

我已经通过 SOF 进行了所有研究,但无济于事。

【问题讨论】:

  • 8000 端口是否有任何监听?
  • 我启用了这个端口并将其添加到我在 wamp 上的 httpd.conf 文件中。但是不,看起来端口没有在监听。知道如何解决这个问题吗?
  • $postUrl 中有什么

标签: php curl localhost wamp


【解决方案1】:

您可能指定了错误的端口。 Apache 监听的默认端口是端口 80(如果我没记错的话,这对于 wampserver 的 Apache 实现也是如此)。试试这个:

function post($data) {
    try {
        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL, '127.0.0.1:80');
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

        $response = curl_exec($ch);

        if ($response === false) throw new Exception(curl_error($ch), curl_errno($ch));

        curl_close($ch);
    } catch (Exception $e) {
        $response = 'Curl failed with error ' . $e->getCode() . ' ' . $e->getMessage();
    }

    return $response;
}

【讨论】:

  • 不,不是这样,我想使用不同的端口,以便我可以使用套接字收听这些帖子。
  • 另外,如果我使用端口 80,由于某种原因,我会被重定向到 localhost 主页,尽管我在调用函数时使用了 echo 语句。没有发布任何内容
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-06
  • 1970-01-01
  • 2013-04-21
  • 1970-01-01
  • 2013-07-31
相关资源
最近更新 更多