php - 如何将 POSTMAN 中的数据转换为 PHP Curl 请求?

标签 php curl

我在 postman 中有一个 API。我想创建一个 CURL 请求并获得正确的响应。这是我的 POSTMAN API。

enter image description here

我成功地得到了这个回应。

"{\"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 调用。我使用了这段代码。

    $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>在 View 中。谁能指导我?提前致谢。我想得到我的 Response .

更新

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

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

最佳答案

It is Very Simple Just Click on Code you will get the code in php.

您将获得多种语言的代码,例如 php、java、ruby、javascript、nodejs、shell、swift、python、C# 等。 enter image description here

enter image description here

关于php - 如何将 POSTMAN 中的数据转换为 PHP Curl 请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40632296/

相关文章:

javascript - 多选数据表中的行,如何从JSON获取一列数据到php

php - 使用php和phpexcel将两个表中的mysql数据导出到excel

php - 提取重复的 MySQL 交叉表

python - Python 的 Requests 模块是否将数据添加到它正在发布的文件中?

swift - PayPal SDK 请求访问 token 返回代码 401(未授权)

php - PHP 脚本可以与 C 程序对话吗?

PHP 在 Apple 产品上提供视频显示

shell - 使用 curl 上传本地目录中的所有文件

curl :(52)如果文件很大,服务器的回复为空

java - 无法使用 JWT 将文件上传到盒子 - curl : (26) failed creating formpost data