带有http身份验证和POST

时间:2016-03-17 09:24:37

标签: php curl http-authentication

我在尝试运行代码时遇到了麻烦。我正在尝试使用带有帖子和http身份验证的cUrl,但我无法让它工作。

我正在使用以下代码:

public function index(){
    $call = $this->call("URL_TO_CALL", $this->getCredentials(), $this->getJson());      
}

private function getCredentials(){
    return "API_KEY_HERE";
}

private function getJson(){
    return $json;
}

private function call($page, $credentials, $post){

    $url = "URL_TO_CALL"; 
    $page = "/shipments"; 
    $headers = array(  
        "Content-type: application/vnd.shipment+json;charset=utf-8", 
        "Authorization: Basic :" . base64_encode($credentials) 
    ); 

    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL,$url); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_TIMEOUT, 60); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 

    // Apply the POST to our curl call 
    curl_setopt($ch, CURLOPT_POST, 1); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post); 

    $data = curl_exec($ch); 

    if (curl_errno($ch)) { 
        print "Error: " . curl_error($ch); 
    } else { 
        // Show me the result 
        var_dump($data); 
        curl_close($ch); 
    }
}

我得到的回应是:

{"errors":[{"code":3001}],"message":"Permission Denied. (insertShipment)"}

我期待着一份货物清单。我在这里做了一些明显不对的事吗?

1 个答案:

答案 0 :(得分:0)

查看代码,您在变量中规定了URL,然后规定了确切的页面,但是当您设置curl的URL时,您只是使用URL变量而不是URL + PAGE变量。