CURL unauthorized 401

时间:2016-04-15 15:01:48

标签: php macos curl

I am using curl but facing unauthorized error even though the username and password set are correct.

$login=base64_encode(sprintf('%s:%s',$this->user,$this->password));
$curl_url = $this->url;
$query= "";

$headers = array(
            sprintf('Authorization: Basic %s',$login),
            sprintf('Accept: %s',$this->format),
            'Content-type: application/x-www-form-urlencoded',
            sprintf('Content-Length: %d',strlen($query))
            );

        $ch = curl_init();
        curl_setopt($ch,CURLOPT_URL,$curl_url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($query));
        curl_setopt($ch, CURLOPT_TIMEOUT, 60);
        $exec = curl_exec($ch);
        curl_close($ch);

URL: "http://localhost/ariaconfig/rest/app_settings_v3/?app=3&os=ios&ver=2.0.1"

If I hit the URL directly on browser, it doesn't ask for credentials but directly says Unauthorized.I am not sure where the error is.

The code is working perfectly fine in Windows but not in OSX

0 个答案:

没有答案