PHP支持的常用数据格式和协议包括:
在PHP中,我们可以使用内置的函数来处理这些数据格式和协议。
JSON:
$json_string = '{"name":"John","age":30,"city":"New York"}';
$json_array = json_decode($json_string, true);
echo $json_array['name']; // 输出 John
XML:
$xml_string = '<root><name>John</name><age>30</age><city>New York</city></root>';
$xml = simplexml_load_string($xml_string);
echo $xml->name; // 输出 John
CSV:
$filename = 'data.csv';
$csv = array_map('str_getcsv', file($filename));
echo $csv[0][0]; // 输出第一行第一列的值
HTTP:
$url = 'http://example.com/api';
$data = array('name' => 'John', 'age' => 30);
$options = array(
'http' => array(
'method' => 'POST',
'content' => http_build_query($data)
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
FTP:
$ftp_server = 'ftp.example.com';
$ftp_user = 'username';
$ftp_pass = 'password';
$file = 'file.txt';
$remote_file = '/path/to/remote/file.txt';
$conn_id = ftp_connect($ftp_server);
ftp_login($conn_id, $ftp_user, $ftp_pass);
ftp_put($conn_id, $remote_file, $file, FTP_ASCII);
ftp_close($conn_id);
以上是使用PHP处理常用数据格式和协议的简单示例。