简体   繁体   English

如何将POSTMAN中的数据转换为PHP Curl Request?

[英]How can I convert data from POSTMAN into PHP Curl Request?

I have an API in postman. 我有邮递员的API。 I want to create a CURL Request and get proper response with it. 我想创建一个CURL请求并获得适当的响应。 This is my POSTMAN API. 这是我的POSTMAN API。

在此输入图像描述

I am successfully getting this response with it. 我成功地得到了这个回应。

"{\"Request\":{\"state\":\"Manama\",\"address\":\"406 Falcon Tower\",\"address2\":\"Diplomatic Area\",\"city\":\"Manama\",\"country\":\"BH\",\"fullname\":\"Dawar Khan\",\"postal\":\"317\"},\"Response\":{\"status\":\"Success\",\"code\":100,\"message\":\"Address is verified\"}}"

Now I want to use this API Call inside my PHP Code. 现在我想在我的PHP代码中使用这个API调用。 I used this code. 我用过这段代码。

    $data = array(
        'Request' => 'ValidateAddress',
        'address' => test_input($form_data->address),
        'secondAddress' => test_input($form_data->secondAddress),
        'city' => test_input($form_data->city),
        'country' => test_input($form_data->country),
        'name' => test_input($form_data->name),
        'zipCode' => test_input($form_data->zipCode),
        'merchant_id' => 'shipm8',
        'hash' => '09335f393d4155d9334ed61385712999'
    );
    $data_string = json_encode($data);

    $url = 'myurl.com/';

    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/json',
        'Content-Length: ' . strlen($data_string))
    );
    $result = curl_exec($ch);
    curl_close($ch);
    $json_result = json_decode($result, true);
    echo '<pre>';print_r($json_result);echo '</pre>';

But I can't see my $json_result. 但我看不到我的$ json_result。 It just echoes <pre></pre> in the view. 它只是在视图中回显<pre></pre> Can anyone guide me? 谁能指导我? Thanks in advance. 提前致谢。 I want to get my Response . 我想得到我的Response

UPDATE UPDATE

I used curl_error and it gives me the following error. 我使用了curl_error,它给了我以下错误。

Curl error: SSL certificate problem: self signed certificate in certificate chain 卷曲错误:SSL证书问题:证书链中的自签名证书

It is Very Simple Just Click on Code you will get the code in php. 它非常简单只需单击代码,您将获得PHP中的代码。

you will get the code in many language like php,java,ruby,javascript,nodejs,shell,swift,pythom,C# etc. 你会得到许多语言的代码,如php,java,ruby,javascript,nodejs,shell,swift,pythom,C#等。 在此输入图像描述

在此输入图像描述

Answer updated as per updated question. 根据更新的问题更新答案。

There are two ways to solve this issue 有两种方法可以解决这个问题

Lengthy, time-consuming yet clean 冗长,耗时但干净

  1. Visit URL in web browser. 在Web浏览器中访问URL。
  2. Open Security details. 打开安全细节。
  3. Export certificate. 出口证明。
  4. Change cURL options accordingly. 相应地更改cURL选项。

     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); curl_setopt($ch, CURLOPT_CAINFO, getcwd() . "/CAcerts/BuiltinObjectToken-EquifaxSecureCA.crt"); 

Quick but dirty 快但脏

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

We are configuring cURL to accept any server(peer) certificate. 我们正在配置cURL以接受任何服务器(对等)证书。 This isn't optimal from a security point of view. 从安全角度来看,这不是最佳选择。

Excerpt from very detailed and precise article with screenshots for better understanding. 摘录非常详细和精确的文章与截图,以便更好地理解。 Kindly refer the same before actually implementing it in production site. 请在生产现场实际实施之前参考相同的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM