php发送json的例子
时间: 2023-04-07 17:01:55 浏览: 63
可以使用以下代码发送JSON数据:
```
<?php
$data = array('name' => 'John', 'age' => 30, 'city' => 'New York');
$json = json_encode($data);
$url = 'http://example.com/api';
$options = array(
'http' => array(
'header' => "Content-type: application/json\r\n",
'method' => 'POST',
'content' => $json,
),
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
$response = json_decode($result);
?>
```
这个例子将一个包含姓名、年龄和城市的数组编码为JSON格式,并将其发送到一个API的URL。在发送请求时,需要设置请求头为“Content-type: application/json”,并将JSON数据作为请求体发送。最后,可以使用file_get_contents()函数获取API的响应,并将其解码为PHP对象。
相关问题
apiv3 php 例子
以下是一个使用 PHP 调用 APIv3 的例子:
```php
<?php
// 填写 APIv3 的接入点和 API 密钥
$endpoint = "https://api.example.com";
$api_key = "your_api_key_here";
// 设置请求参数
$params = array(
"param1" => "value1",
"param2" => "value2",
);
// 创建请求
$ch = curl_init();
// 设置请求 URL
curl_setopt($ch, CURLOPT_URL, $endpoint . "/api/v3/your_api_endpoint");
// 设置请求头
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Authorization: Bearer " . $api_key,
"Content-Type: application/json",
));
// 设置 POST 请求
curl_setopt($ch, CURLOPT_POST, true);
// 设置请求数据
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params));
// 设置响应数据为字符串
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// 发送请求并获取响应
$response = curl_exec($ch);
// 关闭请求
curl_close($ch);
// 处理响应
if ($response === false) {
// 请求失败
echo "Error: " . curl_error($ch);
} else {
// 请求成功
$data = json_decode($response, true);
// 处理响应数据
// ...
}
```
需要替换的内容:
- `$endpoint`:APIv3 的接入点,即 API 的 URL。
- `$api_key`:APIv3 的 API 密钥。
- `$params`:请求的参数,根据 API 的要求填写。
- `"/api/v3/your_api_endpoint"`:APIv3 的具体接口路径,根据 API 的要求填写。
php 表格 定时刷新 ajax 例子
PHP表格定时刷新的例子可以通过Ajax来实现。下面是一个简单的例子:
假设我们有一个包含学生信息的表格,我们要每隔一段时间从数据库中获取最新的学生信息,并用Ajax定时刷新表格。
HTML部分:
```
<table id="student_table">
<tr>
<th>学生ID</th>
<th>学生姓名</th>
<th>年龄</th>
<th>性别</th>
</tr>
</table>
```
JavaScript部分:
```
<script>
function refreshTable() {
// 使用Ajax发送请求
var xhr = new XMLHttpRequest();
xhr.open('GET', 'get_students.php', true);
xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status === 200) {
// 获取服务端返回的数据
var students = JSON.parse(xhr.responseText);
var table = document.getElementById('student_table');
// 清空表格内容
table.innerHTML = '';
// 重新填充表格
for (var i = 0; i < students.length; i++) {
var row = table.insertRow(i+1);
row.insertCell(0).innerHTML = students[i].id;
row.insertCell(1).innerHTML = students[i].name;
row.insertCell(2).innerHTML = students[i].age;
row.insertCell(3).innerHTML = students[i].gender;
}
}
};
xhr.send();
}
// 每隔5秒刷新表格
setInterval(refreshTable, 5000);
</script>
```
PHP部分(get_students.php):
```
<?php
// 假设从数据库中获取学生信息
$students = array(
array('id' => 1, 'name' => '张三', 'age' => 20, 'gender' => '男'),
array('id' => 2, 'name' => '李四', 'age' => 22, 'gender' => '女'),
array('id' => 3, 'name' => '王五', 'age' => 21, 'gender' => '男')
);
// 返回JSON格式的数据
echo json_encode($students);
?>
```
上述代码中,JavaScript部分使用Ajax发送GET请求到`get_students.php`,然后在请求成功时,使用返回的学生信息重新填充表格。页面加载后通过`setInterval`函数每隔5秒自动执行一次`refreshTable`函数,实现定时刷新表格的效果。PHP部分用于模拟从数据库中获取学生信息,并将数据以JSON格式返回给JavaScript。