PHP Curl请求构造求助:课程自动报名URL转Curl实现
PHP cURL Request for Automated Course Registration
Hey there! I’ve run into this exact issue before with array-style query parameters in cURL requests. Let’s get your automated course signup working properly. The key here is handling those nested array parameters (PRESET[Tickets][name][] and PRESET[Tickets][day][]) correctly—PHP’s http_build_query function will be your best friend for this.
Option 1: GET Request (Matching Your Manual URL)
If the registration works via a GET request (like the URL you shared), you can build the full request URL with properly encoded parameters:
<?php // Base URL of the registration page $baseUrl = 'https://URL.com/public/tickets.php'; // Define your parameters as a nested array (matches the URL structure) $params = [ 'PRESET' => [ 'Tickets' => [ 'name' => [''], // Empty value as in your original URL 'day' => ['2018-03-04'] // Target course date ] ] ]; // Convert the array to a URL-encoded query string $queryString = http_build_query($params, '', '&', PHP_QUERY_RFC3986); // Combine base URL and query string $fullUrl = $baseUrl . '?' . $queryString; // Initialize cURL $ch = curl_init($fullUrl); // Set critical cURL options curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Return response instead of printing it curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // Follow redirects (common in registration flows) curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36'); // Mimic a browser // Optional: Disable SSL verification only for testing (NOT for production!) // curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Execute the request $response = curl_exec($ch); // Check for cURL errors if (curl_errno($ch)) { die('cURL Error: ' . curl_error($ch)); } // Clean up cURL resource curl_close($ch); // Process the response (e.g., check if registration succeeded) echo $response; ?>
Option 2: POST Request (More Common for Form Submissions)
Most registration systems use POST requests instead of GET. If your manual flow actually submits a form via POST, use this version instead:
<?php $baseUrl = 'https://URL.com/public/tickets.php'; // Same parameter array as before $postData = [ 'PRESET' => [ 'Tickets' => [ 'name' => [''], 'day' => ['2018-03-04'] ] ] ]; // Encode the POST data into a form-compatible string $postString = http_build_query($postData, '', '&', PHP_QUERY_RFC3986); // Initialize cURL $ch = curl_init($baseUrl); // Set POST-specific options curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POST, true); // Mark request as POST curl_setopt($ch, CURLOPT_POSTFIELDS, $postString); // Attach encoded POST data curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36'); // Set headers to mimic a form submission curl_setopt($ch, CURLOPT_HTTPHEADER, [ 'Content-Type: application/x-www-form-urlencoded' ]); // Execute request and handle response $response = curl_exec($ch); if (curl_errno($ch)) { die('cURL Error: ' . curl_error($ch)); } curl_close($ch); echo $response; ?>
Critical Tips for Success
- Session Persistence: If you need to be logged in to register, add cookie handling to your cURL requests:
curl_setopt($ch, CURLOPT_COOKIEJAR, '/path/to/cookie.txt'); // Save cookies after login curl_setopt($ch, CURLOPT_COOKIEFILE, '/path/to/cookie.txt'); // Load cookies for registration request - SSL Security: For production, never disable
CURLOPT_SSL_VERIFYPEER. Instead, point to a valid CA certificate file:curl_setopt($ch, CURLOPT_CAINFO, '/path/to/cacert.pem'); - Debugging: Use
curl_getinfo($ch)to inspect request details if things aren’t working:print_r(curl_getinfo($ch));
内容的提问来源于stack exchange,提问作者Coen000
相关产品推荐
相关产品推荐

