首页 > 解决方案 > curl - 在邮递员中测试的数据在 php 代码中不起作用

问题描述

我有一个端点和 API 密钥,我正在 Postman 中进行测试,我得到了数据响应。因此,Postman 中的设置正常工作,并且我的 PHP 代码中的 curl 响应在转储 curl 响应时返回 false,并且在触发 API 调用时返回代码 200。

我刚刚从我设置的邮递员标头中实现了一个端点和 API 密钥。我需要两个参数:intcustomerNumber。在邮递员中,它们被设置为原始 JSON 数据。

有人能弄清楚我在做什么错吗?

  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);
}

然后调用它:

 public function validate($customerNumber)
{

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

    return $response;
}

在最终端点:

 $response = $this->get('service')->validate(
        $this->data['customer_number']
    );

标签: phpsymfonycurlsymfony-3.4php-curl

解决方案


推荐阅读