简体   繁体   English

带有http身份验证和POST的PHP Curl请求

[英]PHP Curl request with http authentication and POST

I'm having trouble trying to get my code running. 我在尝试运行我的代码时遇到了麻烦。 I'm trying to use cUrl with a post and http authentication but I'm unable to get it to work. 我正在尝试使用带有帖子和http身份验证的cUrl,但我无法让它工作。

I'm using the following code: 我正在使用以下代码:

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

The response I'm getting is: 我得到的答复是:

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

I was expecting a list of shipments. 我期待发货清单。 Am I doing something obviously wrong here? 我在做一些明显不对的事吗?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM