【问题标题】:How to post JSON POST API in PHP file如何在 PHP 文件中发布 JSON POST API
【发布时间】:2018-04-07 07:53:21
【问题描述】:
{
"id": "4a59a50e-904a-674d-2553-8954ec4a841d",
"name": "JSON API",
"description": "",
"order": [
    "155ae062-f839-ef5a-6c40-61e821202984"
],
"folders": [],
"folders_order": [],
"timestamp": 1522932542555,
"owner": 0,
"public": false,
"requests": [
    {
        "id": "155ae062-f839-ef5a-6c40-61e821202984",
        "headers": "Authorization: Basic OTE5MTAwMTkxY2OjEyMzQ1Ng==\n",
        "headerData": [
            {
                "key": "Authorization",
                "value": "Basic OTE5MTAwMTkxNjY2OyMzQ1Ng==",
                "description": "",
                "enabled": true
            }
        ],
        "url": "https://apiurl.in/bez/api/v1/documents/upload?disableSms=false",
        "queryParams": [
            {
                "key": "disableSms",
                "value": "false",
                "equals": true,
                "description": "",
                "enabled": true
            }
        ],
        "preRequestScript": null,
        "pathVariables": {},
        "pathVariableData": [],
        "method": "POST",
        "data": [
            {
                "key": "jsonData",
                "value": "{\"phoneNumber\":\"9999999999\",\"merchantId\":\"1122112211\",\"amount\":100,\"billDate\":1514891788753,\"gender\":\"male\",\"ageGroup\":\"21-30\",\"dateOfBirth\":\"12-dec-1989\",\"email\":\"dfgd@gfdgfdg.in\",\"address\":\"dfg\"}",
                "description": "Required fileds\n\"phoneNumber\",\"merchantId\",\"amount\",\"billDate\"\noptional fileds\n\"gender\",\"ageGroup\",\"dateOfBirth\",\"email\",\"address\"",
                "type": "text",
                "enabled": true
            },
            {
                "key": "file",
                "value": "",
                "description": "",
                "type": "file",
                "enabled": true
            }
        ],
        "dataMode": "params",
        "tests": null,
        "currentHelper": "basicAuth",
        "helperAttributes": {
            "id": "basic",
            "username": "0101010101",
            "password": "30303030",
            "saveToRequest": true
        },
        "time": 1514892082233,
        "name": "Bill Uploading",
        "description": "",
        "collectionId": "4a59a50e-904a-674d-2553-8954ec4a841d",
        "responses": [],
        "collection_id": "4a59a50e-904a-674d-2553-8954ec4a841d"
    }
]
  }

以上代码在 JSON 文件中。我需要使用 PHP 文件中的代码。

下面的代码是 PHP 的。我不知道如何通过 PHP 发出 POST 请求。

public function sendSmsApi($array)
{
    $data = array();
    $data['apikey'] = Configuration::get('Sendin_Api_Key');
    $data['to'] = $array['to'];
    $data['sender'] = Configuration::get('sender_id');
    $data['message'] = $array['text'];
    $data['type'] = 'xml';

    return Tools::jsonDecode($this->curlRequest($data));
}

如何在 PHP 中包含 JSON 数据并向 API URL 发出 POST 请求。 PHP 代码实际上将消息发送给客户。我想在 PHP 文件中包含 JSON 代码以向 API URL 发送 POST 请求。

【问题讨论】:

  • 你可以发个帖子CURL 让我帮你打出来,我建议谷歌搜索一些关于它的教程。
  • Guzzle 是一个流行的 HTTP 客户端:github.com/guzzle/guzzle
  • 我认为是Tools::jsonEncode

标签: php json api post


【解决方案1】:

根据上述问题中的描述作为解决方案,请尝试执行以下php代码sn-p将包含在json文件中的json数据作为HTTP POST请求的请求体发送。

<?php 
$url = 'http://example.com/get-post.php';
$request_params=file_get_contents('test.json');//test.json contains json data
$headers = array();
$headers[] = 'Content-Type: application/json';
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch,CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_POSTFIELDS, $request_params);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
//execute post
$result = curl_exec($ch) or exit(curl_error($ch));
//close connection
curl_close($ch);
?>

【讨论】:

  • 但是,我通过PHP获取手机号码、金额等参数。那么,我如何将这些值传递给 JSON 文件。
  • 您可以使用 json_encode() 函数将参数编码为 json 字符串,然后将该 json 字符串作为 POST 请求的请求体传递
【解决方案2】:

您可以使用 file_get_contents() 读取 JSON 文件,然后使用 json_decode() 将字符串解析为 PHP 数组。

$json_string = file_get_contents ('data.json');
$json_array = json_decode ($json_string, true);

【讨论】:

    猜你喜欢
    • 2022-01-27
    • 2020-02-19
    • 2021-02-05
    • 1970-01-01
    • 2019-03-29
    • 2017-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多