繁体   English   中英

如何在 PHP 文件中发布 JSON POST API

[英]How to post JSON POST API in PHP file

{
"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 请求。

根据上述问题中提到的描述作为解决方案,请尝试执行以下 php 代码片段,将包含在 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);
?>

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

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

暂无
暂无

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

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