skip to Main Content

I am stuck at a point where the simple URL is working fine in a browser and postman but its not working in Curl called from a PHP code. I am not getting a clue what’s going wrong. Even the php code generated from postman is not working.

My sample URL looks like

https://example.com/WebService/visitorinput.asmx/AddVisitor?CallFromNumber=987xxxxxxx&CallToNumber=886xxxxxxx&CallDateTime=2024-01-10%2018%3A32%3A10&CallType=Missed%20Call&Notes=This%20is%20test%20entry

While the generated postman code is

<?php 

$variables = array(
    "CallFromNumber" => "987xxxxxxx",
    "CallToNumber"   => "886xxxxxxx",
    "CallDateTime"   => "2024-01-10 18:32:10",
    "CallType"       => "Missed Call",
    "Notes"          => "This is test entry",
);

$curl = curl_init();

$url = 'https://example.com/WebService/visitorinput.asmx/AddVisitor?'. http_build_query( $variables ) ; 

curl_setopt_array($curl, array(
  CURLOPT_URL => $url,
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => 'gzip, deflate, br', // Either I add or not result is same
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'GET',
  CURLOPT_HTTPHEADER => [ // Tried to add this header but its same as before
    'Content-Type: application/x-www-form-urlencoded', 
  ] 
));

$response = curl_exec($curl);

url_close($curl);
echo $response;

The requested URL is a web service provided by a different team which built in ASP.net and is working in Postman. In Curl I am getting "403 – Forbidden: Access is denied." while in Postman it’s a json response

2

Answers


  1. Chosen as BEST ANSWER

    I was able to sort it out by passing user agent in header. Complete request look like this

    <?php 
    
    $curl = curl_init();
    
    $url = 'https://example.com/WebService/visitorinput.asmx/AddVisitor?' . http_build_query($variables);
    register_log( "url: ". print_r( $url, true ) );
    
    curl_setopt_array($curl, array(
    CURLOPT_URL => $url,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => 'gzip, deflate, br',
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 0,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => 'GET',
    CURLOPT_HTTPHEADER => [
    'Content-Type: application/x-www-form-urlencoded', 
    'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.15) Gecko/20080623 Firefox/2.0.0.15'
    ]
    ));
    
    $response = curl_exec($curl);
    

  2. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search