如何在PHP文件

时间:2018-04-07 07:53:22

标签: php json api post

{
"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 OTE5MTAwMTkxNjY2OjEyMzQ1Ng==\n",
        "headerData": [
            {
                "key": "Authorization",
                "value": "Basic OTE5MTAwMTkxNjY2OjEyMzQ1Ng==",
                "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\":\"murali@billez.in\",\"address\":\"Hyderabad\"}",
                "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代码以将A POST请求发送到API URL。

2 个答案:

答案 0 :(得分:0)

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

  for(i in 1:length(coinData))
  {  
      coinName=coinData[[i]]$coin  
      write.csv(coinData[[i]],file=paste(coinName,".csv",sep=""))  
  }  

答案 1 :(得分:0)

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