0

I have an endpoint and API key that I am testing in Postman and I get a response with data. So, setting in Postman is working as it should and my curl response in my PHP code is returning false on dumping curl response and code 200 when triggering API call.

I just implemented an endpoint and API key from my postman headers that I set. I need two parameters: int and customerNumber. In postman, they are set as raw JSON data.

Can someone figure out what am I doing wrong?

  public function call($action, $parameter)
{
    $endpoint = 'https://api.myendpoint.co.uk/full-dinner-serve/la/la/land';
    $apiKey = 'NNOsndosfl14FS45ddgdg1';

    $ch = curl_init();

    $headers = array();
    $headers[] = 'Content-Type: application/json';
    $headers[] = 'x-api-key: ' . $apiKey;

    curl_setopt($ch, CURLOPT_URL, $endpoint);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");

    $postBody = array (
        'action' => $action,
        'customerNumber' => $parameter
);

    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postBody));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);

    $response = curl_exec($ch);

    return json_decode($response, true);
}

And then calling it:

 public function validate($customerNumber)
{

    $response = $this->call(
        'init',
        $customerNumber
    );

    return $response;
}

And in final endpoint:

 $response = $this->get('service')->validate(
        $this->data['customer_number']
    );
Mujahid Bhoraniya
  • 1,518
  • 10
  • 22
JoniJon
  • 13
  • 1
  • 1
  • 6
  • Make up your mind whether you want to set CURLOPT_HEADER to 0 or true? – CBroe Mar 16 '20 at 11:54
  • Thnak you. I removed that part. I think that is not an issue. Error remained the same. @CBroe – JoniJon Mar 16 '20 at 12:05
  • _“and my curl respone in my PHP code is returning false”_ - what _exactly_ is returning false? The curl_exec itself? Or maybe probably rather the attempt at decoding whatever was returned, as JSON? Well then check _what_ you are trying to decode as JSON there first of all. – CBroe Mar 16 '20 at 12:07
  • $response = curl_exec($ch); is returning false. @CBroe :/ – JoniJon Mar 16 '20 at 12:09
  • And curl_errno / curl_error say what? – CBroe Mar 16 '20 at 12:11
  • Request Error:SSL certificate problem: unable to get local issuer certificate @CBroe – JoniJon Mar 16 '20 at 12:16

0 Answers0