-1

I have got a problem with a PHP cURL request, it displays the 403 error.

In Postman and a browser the URL works fine and Postman produced the PHP cURL code which is below but show 403 error anyway. I guess there is a cookie being generated in the same call, but CURLOPT_COOKIESESSION is not working. Can anyone please help with the code below to get a JSON data? Many thanks.

    $curl = curl_init();
    curl_setopt_array($curl, array(
      CURLOPT_URL => 'https://www.britishairways.com/solr/lpbd/lpfdestinations?fq=departure_city:LON+AND+arr_city_name_search:***+AND+trip_type:RT+AND+number_of_nights:7+AND+cabin:M&facet.pivot=arrival_city,is_sales_fare&facet=true',
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_ENCODING => '',
      CURLOPT_MAXREDIRS => 10,
      CURLOPT_TIMEOUT => 0,
      CURLOPT_COOKIESESSION => true,
      CURLOPT_FOLLOWLOCATION => true,
      CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
      CURLOPT_CUSTOMREQUEST => 'GET',
    ));
    $response = curl_exec($curl);
    curl_close($curl);
    echo $response;
Sergey Panteleev
  • 122
  • 1
  • 1
  • 10
qqruza
  • 1,385
  • 4
  • 20
  • 41
  • If you were to use `$error=curl_error( $curl );` and then print that you would find `"SSL certificate problem: unable to get local issuer certificate"` - the endpoint is `https` yet your code does not attempt to negotiate over SSL. If you modify the url to `http://....` then your code will return the json data as you expect but it would be better to continue using the SSL secured endpoint but configure curl correctly to do so. – Professor Abronsius Jun 21 '22 at 07:14
  • Hi I still have no luck, I am on PHP 8.1 and the cURL still gives me 403 error... – qqruza Jun 23 '22 at 20:05

1 Answers1

-1
Try this, It's working fine.    



        

$curl = curl_init();
            curl_setopt_array($curl, array(
              CURLOPT_URL => 'https://www.britishairways.com/solr/lpbd/lpfdestinations?fq=departure_city:LON+AND+arr_city_name_search:***+AND+trip_type:RT+AND+number_of_nights:7+AND+cabin:M&facet.pivot=arrival_city,is_sales_fare&facet=true',
              CURLOPT_RETURNTRANSFER => true,
              CURLOPT_ENCODING => '',
              CURLOPT_MAXREDIRS => 10,
              CURLOPT_TIMEOUT => 0,
              CURLOPT_COOKIESESSION => true,
              CURLOPT_FOLLOWLOCATION => true,
              CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
              CURLOPT_CUSTOMREQUEST => 'GET',
            ));
            $response = curl_exec($curl);
            curl_close($curl);
            echo json_encode($response);
sri vijay
  • 1
  • 1
  • 1
    Please add explanation what was wrong with OP's approach – Marcin Orlowski Jun 21 '22 at 06:18
  • hi @sri-vijay, I have checked your code on https://phpsandbox.io/ and it still shows 403. – qqruza Jun 21 '22 at 06:20
  • I'm not adding anything. I'm just running your code with the json_encode process. It's working fine for me. – sri vijay Jun 21 '22 at 06:28
  • I can see no difference here to the original - can you explain what you changed> – Professor Abronsius Jun 21 '22 at 06:48
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Cristik Jun 21 '22 at 07:43
  • 1
    The reported problem is a 403 response from the remote server. Using exactly the same request code, but using `json_encode()` on the response won't change that. – Don't Panic Jun 21 '22 at 13:50