Postman请求参数转PHP数组及tags数组请求报错的替代方案
Got it, let's figure out why your PHP request is throwing that "'tags' value is not a valid array" error—even though Postman works perfectly. Here's what's going on and how to fix it step by step:
Why This Happens
The core issue is almost always how your PHP code is encoding and sending the tags array compared to Postman. Postman is likely sending the data as raw JSON (with the tags field as a proper JSON array), but PHP might be defaulting to form-encoded data (which formats arrays as tags[]=sports&tags[]=news—a format the API doesn't recognize as a valid array).
Converting Postman's Request to a PHP Array
First, let's mirror exactly what Postman is doing. If your Postman request uses a raw JSON body like this:
{ "name": "Jane Smith", "email": "jane@example.com", "tags": ["sports", "news"] }
Your PHP array structure is already correct:
$payload = [ 'name' => $name, 'email' => $email, 'tags' => ['sports', 'news'] // This matches Postman's JSON array ];
The problem isn't the array itself—it's how you're sending it to the API.
Fixing the "'tags' value is not a valid array" Error (Solutions)
Solution 1: Send the Data as JSON (Most Common Fix)
The API probably expects JSON-formatted data. To do this in PHP:
- Encode your payload array to a JSON string with
json_encode() - Set the
Content-Type: application/jsonheader in your request
Example with cURL:
$name = "Jane Smith"; $email = "jane@example.com"; $payload = [ 'name' => $name, 'email' => $email, 'tags' => ['sports', 'news'] ]; $jsonPayload = json_encode($payload); // Initialize cURL $ch = curl_init('https://your-api-endpoint-here.com'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Set required headers for JSON curl_setopt($ch, CURLOPT_HTTPHEADER, [ 'Content-Type: application/json', 'Accept: application/json' ]); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonPayload); // Execute and get response $response = curl_exec($ch); curl_close($ch); // Handle response var_dump($response);
Example with Guzzle (Simpler):
If you're using Guzzle HTTP client, it handles JSON encoding automatically when you use the json option:
use GuzzleHttp\Client; $client = new Client(); $response = $client->post('https://your-api-endpoint-here.com', [ 'headers' => [ 'Accept' => 'application/json', ], 'json' => [ 'name' => $name, 'email' => $email, 'tags' => ['sports', 'news'] ] ]); $body = $response->getBody()->getContents(); var_dump($body);
Solution 2: Use Form-Encoded Array Format (If API Expects It)
If Postman is using form-data or x-www-form-urlencoded instead of JSON, the API might expect arrays in the tags[] format. Adjust your PHP payload like this:
$payload = [ 'name' => $name, 'email' => $email, 'tags[]' => ['sports', 'news'] // Add [] to the key ]; // Encode for form data $formData = http_build_query($payload);
Then send the request with Content-Type: application/x-www-form-urlencoded header.
Solution 3: Fallback to Comma-Separated String (If API Accepts It)
Some APIs expect array values as comma-separated strings instead of actual arrays. If the API allows this, convert your PHP array to a string:
$payload = [ 'name' => $name, 'email' => $email, 'tags' => implode(',', ['sports', 'news']) // Converts to "sports,news" ];
Send this payload either as JSON or form data, depending on what the API expects.
Quick Validation Tip
Before sending the request, print the encoded payload to make sure it matches what Postman sends:
echo json_encode($payload); // Should output: {"name":"Jane Smith","email":"jane@example.com","tags":["sports","news"]}
Compare this to Postman's raw JSON body—they should be identical.
内容的提问来源于stack exchange,提问作者Umair Zahid

