-1

I want to post data using PHP curl with basic authentication. Here is my code but I am not able to post using API. But it's working with postman.

$urlp = "URL/sap/opu/odata/sap/ZOSA_UPLOAD_SRV/AccountOrderSet?sap-client=900";
$username = '*****';
$password = '********';


$ch = curl_init ();
  curl_setopt($ch,CURLOPT_URL,$urlp);
  curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
  curl_setopt($ch,CURLOPT_MUTE,1);
  curl_setopt($ch,CURLOPT_USERPWD,"$username:$password");
  curl_setopt($ch,CURLOPT_USERAGENT,"Proventum Proxy/1.0 (Linux)");
  curl_setopt($ch, CURLOPT_TIMEOUT, 10); //times out after 10s
  curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
 curl_setopt($chp, CURLOPT_HTTPHEADER, array(
                'Content-Type: application/json',
                'Accept: application/json',
                'X-Requested-With: application/json')
            );
            
  if ($HTTP_REFERER) {
    curl_setopt($ch,CURLOPT_REFERER, $HTTP_REFERER);
  }
  
      curl_setopt($ch,CURLOPT_POST,1);
      curl_setopt($ch,CURLOPT_POSTFIELDS,$datap_string);
    
  $result = curl_exec ($ch);
  print $result;
  curl_close($ch);
Marios
  • 26,333
  • 8
  • 32
  • 52
Rajkeshar
  • 75
  • 10

1 Answers1

0

Here you are, add to the HTTPHEADER option:

Authorization: Basic base64encode(username:password)

for example:

curl_setopt($chp, CURLOPT_HTTPHEADER, array(
            'Authorization: Basic dXNlcm5hbWU6cGFzc3dvcmQ=',
            'Content-Type: application/json',
            'Accept: application/json',
            'X-Requested-With: application/json')
        );
AnhTN
  • 314
  • 2
  • 4
  • 14