web API POST curl返回'找不到与请求URI匹配的任何内容'

时间:2015-06-01 12:09:41

标签: php json curl

我尝试在php中使用json编码和$ POST方法访问web api。经过几次尝试后,我收到了这条消息:

  

未找到 - 服务器未找到与请求URI匹配的任何内容   你可以在这里获得技术细节。请继续访问我们的网站   主页。

为什么呢?谢谢,我的代码是:

 <?php

    ini_set('display_errors', 1);
    error_reporting(E_ALL);

    //DADOS DE ACESSO
    $id = "xxx";
    $retailerName = "xxx";

    $key = "xxx";

    //PRODUTO
    //vars of product:fictional data
    $upc = "799366289999";
    $amount = "20.00";

    //URL DE ACESSO
    $function = "requestActivateCode";

    //usado pra criar a canonical
    $resPath = "/v1/activateCode";

    $url = "https://111.222.333.444/v1/";

    $urlCall = $url.$function;

    //HTTP HEADERS
    $date = gmdate("M, d Y H:i:s \G\M\T", time());
    $contentType = "Content-Type: application/json; charset=UTF-8";
    $accept = "Accept: application/json";
    $xIncommDateTime = substr(substr_replace(date('c'), substr(round(microtime(), 3), 1, 8), 19, 0), 0, -6) . "Z";

    //authorization
    $type = "application/json; charset=UTF-8";
    $canonical = ($xIncommDateTime) + ($type) + ($resPath);

    $signature = hash_hmac ('sha1', $key, $canonical);
    $idEncoded = base64_encode($id);
    $signatureEncoded = base64_encode($signature);
    $authorization = "Incomm ". $idEncoded .':'. $signatureEncoded;

    $header = array("Date: ".$date, $contentType, $accept, "X-Incomm-DateTime: ".$xIncommDateTime, "Authorization: ".$authorization);


    //PARAMS example
    $params = array("RetailTransactionRequest" =>
        array(
            "code" => "0000002381237220",
            "amount" => $amount,
            "upc" => $upc,
            "transactionID" => "1234",
            "dateTime" => $xIncommDateTime,
            "retailerName" => $retailerName
        )
    );

    $request = json_encode($params);


    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $urlCall);
    //curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);  
    //curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
    //curl_setopt($ch, CURLOPT_SSLVERSION, 1);
    //curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
    curl_setopt($ch, CURLOPT_PORT, 443);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT ,0);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
    echo curl_setopt($ch, CURLINFO_HEADER_OUT, true); // enable tracking
    $result = curl_exec($ch);
    echo $result;

    $headerSent = curl_getinfo($ch, CURLINFO_HEADER_OUT ); // request headers

    echo "<pre>";
    print_r($headerSent);
    echo "</pre>";

    if($result === false)
    {
        echo 'Curl error: ' . curl_error($ch);
    }
    else
    {
        echo 'Operation completed without any errors';
        echo "<pre>";
        print_r($result);
        echo "</pre>";
    }

netstat -l ip我的端口443正在侦听,但主机在ping中。我想知道如何解决问题,谢谢

1 个答案:

答案 0 :(得分:0)

如果代码完全是显示的,我不认为您的服务器的IP地址是正确的。不使用示例中使用的IP地址(111.222.333.444),而是替换您站点的IP地址或站点的域名。

$url = "https://111.222.333.444/v1/"