<?php
$codes        = [];
$responseCode = 0;

$i = 0;
while ( $responseCode != 200 && $i < 100 ) {
    // $url = "https://www.homedepot.com/p/Hampton-Bay-Blue-8-ft-x-10-ft-Traditional-Medallion-Indoor-Outdoor-Area-Rug-3133087/330480540";
    // $url = "https://www.build.com/axor-12595/s2037699?uid=4885026&searchId=goXqprz2j9";
    // $url = "https://www.lowes.com/pd/ORTHO-Groundclear-Ready-to-Use-1-Gallon-Weed-and-Grass-Killer/1000833738";
    // $url = "https://www.lowes.com/pl/cooktops/electric-cooktops/4294715799";
    $url = "https://www.sherwin-williams.com/homeowners/products/emerald-interior-acrylic-latex-paint?colorPartNumber=SW7568&colorType=PALETTE";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, [
        'Content-Type: text/html', 
        'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.2 Safari/605.1.15', 
        'X-Forwarded-For: 174.80.28.191', 
        'Referer: https://www.google.com/search?q=lowes',
        // 'Cookie: sn=2333'
        // 'Accept: text/html,application/xhtml+xml,application/xml;',
        // 'Accept-Encoding: gzip, deflate, br',
        // 'Accept-Language: en-US,en;q=0.9',
        // 'Priority: u=0, i',
        // 'Sec-Fetch-Dest: document',
        // 'Sec-Fetch-Mode: navigate',
        // 'Sec-Fetch-Site: none',
        // 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.2 Safari/605.1.15'
    ]);
    // $response = json_decode(curl_exec($ch));
    $response = curl_exec($ch);
    $responseCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    $codes[] = $responseCode;
    
    curl_close($ch);
    if ( $responseCode == 200 ) :
?>
        <?php echo "responseCode: $responseCode"; ?>
        <br><br>
        <pre><?php echo htmlentities($response); ?></pre>
<?php
        echo implode('<br>', $codes);
        exit;
    endif;
    $i++;
}
echo implode('<br>', $codes);
exit;
?>





<?php
$url = "https://www.homedepot.com/p/Liberty-Align-Right-Large-Cabinet-Hardware-Installation-Template-AN6516C-CL-U/308682642";
// $url = "https://www.build.com/axor-12595/s2037699?uid=4885026&searchId=goXqprz2j9";
// $url = "https://www.naturalelementhomes.com";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: text/html', 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.2 Safari/605.1.15']);
// $response = json_decode(curl_exec($ch));
$response = curl_exec($ch);
$responseCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
?>
<?php echo "responseCode: $responseCode"; ?>
<br><br>
<pre><?php htmlspecialchars(print_r($response)); ?></pre>
<?php exit; ?>
<?php
$payload = [
    'max_tokens' => 4096,
    'messages' => [
        [
            'role' => 'user',
            'content' => [
                [
                    'type' => 'text',
                    'text' => 'Analyze the following HTML and tell me the product name, description, category, price, and provide a url to the best version of the primary product image: '.$html
                ],
                // [
                //     'type' => 'text',
                //     'source' => [
                //         'type' => 'base64',
                //         'media_type' => 'text/html',
                //         'data' => base64_encode(file_get_contents('https://www.build.com/trimlite-2668138-8405gl/s1652126?uid=3912405'))
                //     ]
                // ]
            ]
        ]
    ],
    'model' => 'claude-3-5-sonnet-20241022',
    // 'system' => 'Your job is to help automate a time-consuming part of custom home building. You analyze floors plans and provide exactly what is requested in a format that can be used programmatically'
];

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.anthropic.com/v1/messages");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json', 'x-api-key: '.CLAUDE_API_TOKEN, 'Anthropic-Version: 2023-06-01', 'anthropic-beta: pdfs-2024-09-25,prompt-caching-2024-07-31']);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload));

$response = json_decode(curl_exec($ch));
$responseCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

?>
<pre>
    <?php print_r($response); ?>
</pre>
