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

PHP读取文本单行为变量及实现CURL代理每5次轮询的方法

Hey there! Let's break down your PHP problems and fix them step by step. First, your proxy rotation logic has a few bugs, and we'll also cover proper single-line file reading for your use case.

1. Fixing Proxy Rotation (5 Uses per Proxy)

Your current binsforeveryoneproxys() function is returning the entire array of proxies instead of a single one, which is why cURL can't use it. To implement "5 uses per proxy" rotation, we need to track two things: which proxy we're currently using, and how many times we've used it. We can use static variables inside the function to preserve this state between calls.

Here's the corrected function:

function getCurrentProxy() {
    $fileName = 'joestar.txt';
    // Read proxies into an array, ignoring newlines and empty lines
    $proxies = file($fileName, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
    
    // Static variables retain their values between function calls
    static $currentProxyIndex = 0;
    static $useCount = 0;
    
    // If we've used the current proxy 5 times, switch to the next one
    if ($useCount >= 5) {
        $currentProxyIndex++;
        // Loop back to the first proxy if we reach the end of the list
        if ($currentProxyIndex >= count($proxies)) {
            $currentProxyIndex = 0;
        }
        $useCount = 0; // Reset counter for the new proxy
    }
    
    // Increment the use count for the current proxy
    $useCount++;
    
    // Return the single proxy string cURL needs
    return $proxies[$currentProxyIndex];
}

How to Use This with cURL

Every time you make a cURL request, call getCurrentProxy() to get the active proxy. If you're making multiple requests, this function will automatically switch proxies after 5 uses:

// Initialize cURL once (efficient for multiple requests)
$ch = curl_init();

// Set base cURL options (only need to set these once)
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36');
// Note: Your original code used `getcwd().'/cookie.txt'` as a user agent — this is invalid! Use a real user agent string.
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'accept: application/json',
    'content-type: application/x-www-form-urlencoded',
    'origin: https://your-origin-domain.com', // Fill in your actual origin
    'referer: https://your-referer-page.com' // Fill in your actual referer
));

// Example: Make 15 requests (cycles through 3 proxies, 5 times each)
for ($i = 1; $i <= 15; $i++) {
    // Get the current proxy and set it for this request
    $currentProxy = getCurrentProxy();
    curl_setopt($ch, CURLOPT_PROXY, $currentProxy);
    
    // Set request-specific options
    curl_setopt($ch, CURLOPT_URL, 'https://your-target-api.com'); // Fill in your target URL
    curl_setopt($ch, CURLOPT_POSTFIELDS, 'your-post-data-here'); // Fill in your POST parameters
    
    // Execute and handle the result
    $result = curl_exec($ch);
    echo "Request $i using proxy: $currentProxy\n";
    var_dump($result);
}

// Clean up cURL resource
curl_close($ch);

Key Fixes & Notes:

  • Static Variables: $currentProxyIndex and $useCount remember their values between function calls, so we don't lose track of our rotation state.
  • Valid User Agent: Replace the invalid user agent string with a real one to avoid being blocked by target servers.
  • Empty Line Handling: Added FILE_SKIP_EMPTY_LINES to ignore blank lines in your proxy file, preventing errors from empty proxy entries.
2. Reading Single Lines from a Text File

If you just need to read a single line (not for rotation), you have two simple options:

  • Read one line at a time with fgets():
    $fp = fopen('joestar.txt', 'r');
    if ($fp) {
        $singleLine = fgets($fp); // Reads the first line of the file
        fclose($fp);
        // Use $singleLine as your variable
        echo $singleLine;
    }
    
  • Read all lines into an array and pick a specific one:
    $lines = file('joestar.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
    $firstLine = $lines[0]; // Get the first line
    $randomLine = $lines[array_rand($lines)]; // Get a random line from the file
    

内容的提问来源于stack exchange,提问作者Henry cy

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.07 12:17:40