You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel中Guzzle HTTP调用API未返回预期email_token问题排查

Troubleshooting Missing email_token in Guzzle GET Response

Hey there, let's break down why you're getting a 200 OK response but not the expected email_token from your API call. Here are the most likely issues and fixes to try:

1. Query Parameters Aren't Being Passed Correctly

Looking at your code, you're passing the email parameter directly in the request options array—but Guzzle requires GET query parameters to be nested under the query key. Without this wrapping, your API probably isn't receiving the email value at all, so it returns a default 200 response without the token.

Fix your request options like this:

$res = $client->request('GET', 'https://www.example.com/apps/api/Services/Email/Opting', [
    'query' => ['email' => $email] // Wrap params in the 'query' sub-array
]);

2. You're Not Explicitly Requesting a JSON Response

Many APIs return HTML or plain text by default unless you tell them you want JSON. If your response body isn't in JSON format, parsing it won't reveal the token field you're expecting.

Add an Accept header to enforce JSON output:

$res = $client->request('GET', 'https://www.example.com/apps/api/Services/Email/Opting', [
    'query' => ['email' => $email],
    'headers' => [
        'Accept' => 'application/json'
    ]
]);

3. You're Not Parsing the Response Body as JSON

Calling dd($res->getBody()) gives you the raw response string. You need to decode it into an array or object to properly check for the token field.

Parse the response and validate the token exists:

$responseData = json_decode($res->getBody(), true);
if (isset($responseData['token'])) {
    // Success! Use the token for your unsubscribe POST request
    $unsubscribeRes = $client->request('POST', 'YOUR_UNSUBSCRIBE_API_ENDPOINT', [
        'form_params' => [
            'token' => $responseData['token'],
            // Add other required unsubscribe parameters here
        ]
    ]);
} else {
    // Debug: See what the API actually returned
    dd('No token found. Full response data:', $responseData);
}

4. Missing Authentication or Required Headers

Double-check the API documentation—some services require an API key, bearer token, or other custom headers to return sensitive data like email_token. If you're missing these, the API might return a 200 but with limited, non-sensitive data.

Example with an API key header (adjust based on your API's requirements):

'headers' => [
    'Accept' => 'application/json',
    'Authorization' => 'Bearer YOUR_API_KEY' // Or 'Token YOUR_API_KEY' depending on the API
]

Quick Debug Step: Test Manually with curl/Postman

Before digging deeper into code, test the request manually using curl or Postman. This will confirm if the API returns the token when parameters are correctly formatted. For example:

curl "https://www.example.com/apps/api/Services/Email/Opting?email=test@example.com" -H "Accept: application/json"

If this returns the token, the issue is definitely in how Guzzle is constructing the request.


内容的提问来源于stack exchange,提问作者Matt Larsuma

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 06:49:39