curl – Data tested in postman wont work in php code

I have an endpoint and api key that I am testing in Postman and I get response with data. So, my setting in Postman is working as it should and my curl respone in my PHP code is returning false on dupming curl response and code 200 when trigering 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);
    curl_setopt($ch, CURLOPT_HEADER, true);

    $response = curl_exec($ch);
    $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

    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']
    );

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *