Curl Post与php总是在localhost上失败

时间:2014-07-29 23:49:20

标签: php curl localhost wamp

我使用wamp在我的本地计算机上使用php运行Apache。我确定dll文件在php.ini中取消注释(在apache和php bin文件夹下)。我有以下功能,每当我调用时,我都会收到错误:

卷曲失败,错误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对此进行了研究,但无济于事。

1 个答案:

答案 0 :(得分:0)

您可能指定了错误的端口。 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;
}