PHP Curl 请求与 http 身份验证和 POST


PHP Curl request with http authentication and POST

我在尝试运行代码时遇到问题。我正在尝试将 cUrl 与帖子和 http 身份验证一起使用,但我无法让它工作。

我正在使用以下代码:

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)"}

我期待一份发货清单。我在这里做错了什么吗?

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