如何将数据从POSTMAN转换为PHP Curl Request? [英] How can I convert data from POSTMAN into PHP Curl Request?

查看:52
本文介绍了如何将数据从POSTMAN转换为PHP Curl Request?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

邮递员中有一个API.我想创建一个CURL请求并获得正确的响应.这是我的POSTMAN API.

I have an API in postman. I want to create a CURL Request and get proper response with it. This is my 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\"}}"

现在,我想在我的PHP代码中使用此API调用.我使用了这段代码.

Now I want to use this API Call inside my PHP Code. 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>';

但是我看不到我的$ json_result.它只是在视图中回显< pre></pre> .谁能指导我?提前致谢.我想得到我的 Response .

But I can't see my $json_result. It just echoes <pre></pre> in the view. Can anyone guide me? Thanks in advance. I want to get my Response.

更新

我使用了curl_error,它给了我以下错误.

I used curl_error and it gives me the following error.

卷曲错误:SSL证书问题:证书链中的自签名证书

Curl error: SSL certificate problem: self signed certificate in certificate chain

推荐答案

根据已更新的问题更新答案.

Answer updated as per updated question.

有两种方法可以解决此问题

There are two ways to solve this issue

冗长,耗时但干净的

  1. 在网络浏览器中访问URL.
  2. 打开安全性详细信息.
  3. 出口证书.
  4. 相应地更改cURL选项.

  1. Visit URL in web browser.
  2. Open Security details.
  3. Export certificate.
  4. Change cURL options accordingly.

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

快速但肮脏

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

我们正在配置 cURL 以接受任何服务器(对等)证书.从安全角度来看,这不是最佳选择.

We are configuring cURL to accept any server(peer) certificate. 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.

这篇关于如何将数据从POSTMAN转换为PHP Curl Request?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆