如何用Curl调用PayPal订阅API查询已有计费计划信息
Fixing PayPal Billing Plan Lookup with cURL in PHP
Hey there, let's work through why your cURL command isn't returning a response and fix it up!
The Main Issues in Your Current Command
- Wrong HTTP Method: Looking up a billing plan uses a
GETrequest, notPOST. PayPal'sv1/billing/plans/{plan_id}endpoint only acceptsGETfor retrieving plan details (POST is reserved for creating or updating plans). - Duplicate Header: You've included
Content-Type: application/jsontwice—this is unnecessary, and for GET requests, you don't even need this header at all. - Formatting Glitches: The backslashes in your command have extra spaces after them, which can cause parsing issues when
exec()runs the command.
Correct cURL Command (Standalone)
First, here's the fixed standalone cURL command you can test directly in your terminal:
curl -v -X GET https://api.sandbox.paypal.com/v1/billing/plans/____PLANID_____ \ -H "Accept-Language: en_US" \ -H "Authorization: Bearer YOUR_ACCESS_TOKEN"
Just replace ____PLANID_____ with your actual plan ID and YOUR_ACCESS_TOKEN with the token from paypal_tokenix().
Fixed PHP Code Using exec()
If you want to keep using exec() in PHP, here's the cleaned-up version with safety and formatting fixes:
// Grab your PayPal access token $paypal_token = paypal_tokenix(); // Replace with your actual plan ID $plan_id = '____PLANID_____'; // Build the cURL command with proper escaping $paypalcurl = 'curl -v -X GET "https://api.sandbox.paypal.com/v1/billing/plans/' . $plan_id . '" \ -H "Accept-Language: en_US" \ -H "Authorization: Bearer ' . escapeshellarg($paypal_token) . '"'; // Execute the command and capture output/errors $output_lines = []; $return_status = 0; exec($paypalcurl . ' 2>&1', $output_lines, $return_status); // Combine output lines and parse JSON $raw_response = implode("\n", $output_lines); $plan_result = json_decode($raw_response, true); // Debug the result var_dump($plan_result);
escapeshellarg()ensures your access token is safely escaped to avoid command injection risks.- Combining output lines into a single string fixes issues with split response data that can break JSON parsing.
Better Alternative: Use PHP's Built-in cURL Extension
Using PHP's native cURL functions is more reliable than calling system cURL via exec()—it's easier to debug and avoids environment-specific issues. Here's how to do it:
$plan_id = '____PLANID_____'; $paypal_token = paypal_tokenix(); // Initialize cURL session $ch = curl_init(); // Set cURL options curl_setopt($ch, CURLOPT_URL, "https://api.sandbox.paypal.com/v1/billing/plans/{$plan_id}"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Return response as string curl_setopt($ch, CURLOPT_HTTPHEADER, [ 'Accept-Language: en_US', "Authorization: Bearer {$paypal_token}" ]); curl_setopt($ch, CURLOPT_VERBOSE, true); // Enable verbose logging for debugging (optional) // Execute the request $response = curl_exec($ch); // Check for cURL errors if (curl_errno($ch)) { echo "cURL Error: " . curl_error($ch); } // Close the session curl_close($ch); // Parse and debug the result $plan_result = json_decode($response, true); var_dump($plan_result);
内容的提问来源于stack exchange,提问作者Jintor
相关产品推荐
相关产品推荐

