PHP cURL GET请求:您无权访问此资源

I am facing varied issue. I am able to get response in POSTman but getting below error while using PHP code.

You are not authorized to access this resource

code as below:

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_USERPWD, $username . ':' . $password);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER,     array('Content-Type: application/xml','Accept: application/xml')); 
$data = curl_exec($ch);
curl_close($ch);

Unfortunately, different cURL versions behave slightly different and so there is not one valid answer but several approaches that work for different cURL versions.

Here are two suggestions:

From Problems with username or pass with colon when setting CURLOPT_USERPWD

Try adding curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);, or instead CURLAUTH_BASIC.

Something that should always work:

If it won't help, add username and password directly into url like https://user:pass@host.com/path.

You shouldnt turn off certificate verification, instead, get a valid cert, they are for free using letsencrypt.

<?php

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_USERPWD, $username . ':' . base64_encode($password)); //here is the change
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER,     array('Content-Type: application/xml','Accept: application/xml'));
$data = curl_exec($ch);
curl_close($ch);

This is a really longshot and i know that but i have seen quite a few API's that work like that and since the OP seems not to have the documentation of the API i will post this as an answer in case it helps him solve his issue.

If above does not work try to base64_encode($username) as well