Home  >  Article  >  Backend Development  >  How to send array in php

How to send array in php

PHPz
PHPzOriginal
2023-04-26 14:18:46574browse

In web development, it is often necessary to send an array to other websites or servers. At this time, we can use the functions provided by the PHP language to implement this process. This article will introduce how to use PHP to send arrays to other servers.

1. Using the HTTP protocol

In the HTTP protocol, you can use GET and POST to send data. If you need to send a large array, it is recommended to use the POST method. The following are the steps to use the POST method to send data:

1. Create an associative array

$data = array(
    'name' => '张三',
    'age' => 22,
    'gender' => '男',
    'address' => '北京市海淀区xxx街道'
);

2. Use the curl library provided by PHP to send data

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.example.com/receive_data.php');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);

3. After receiving On the data page, use PHP's $_POST variable to read the sent data

$name = $_POST['name'];
$age = $_POST['age'];
$gender = $_POST['gender'];
$address = $_POST['address'];

2. Use JSON format

Another popular way to send data is to use JSON format. The following are the steps to send data using JSON format:

1. Create an associative array

$data = array(
    'name' => '张三',
    'age' => 22,
    'gender' => '男',
    'address' => '北京市海淀区xxx街道'
);

2. Convert the array into a JSON format string

$json_data = json_encode($data);

3. Use PHP The provided curl library sends data

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.example.com/receive_data.php');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Content-Length: ' . strlen($json_data)));
$response = curl_exec($ch);
curl_close($ch);

4. On the page receiving data, use the JSON decode function to read the sent data

$data_json = file_get_contents("php://input");
$data = json_decode($data_json, true);
$name = $data['name'];
$age = $data['age'];
$gender = $data['gender'];
$address = $data['address'];

Summary

This article introduces two There are two commonly used methods to send arrays to other servers, one is to use the POST method of the HTTP protocol, and the other is to use the JSON format. Among them, the JSON format is more flexible and can describe more complex data structures, but the corresponding code is also more. If you just need to send a simple associative array, the POST method of the HTTP protocol is more convenient and concise to use. It should be noted that when using the POST method to send data, you need to use PHP's $_POST variable on the page that receives the data to read the sent data. When using JSON format, Content-Type and Content-Length need to be set in the request header.

The above is the detailed content of How to send array in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn