skip to Main Content

As the title says, why won’t this code work?

$homepage = file_get_contents('https://www.google.com');
    echo $homepage;
    $file = 'code.txt';
    
    file_put_contents($file, $homepage);

Gives me the error
"PHP Warning: file_get_contents(https://www.google.com): Failed to open stream: No such file or directory in C:UsersStevepublic_htmlindex.php on line 11"

Thank you for your time

I tried the above code and have tried other solutions, my allow_fopen is set to on

2

Answers


  1. Chosen as BEST ANSWER

    The website uses methods to stop it being scraped, it required the Cookie in the Header

    $opts = [
        "http" => [
            
           'method' => 'GET',
            'header' => "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7rn" .
                        "Accept-language: enrn" .
                        "Host: www.checkatrade.comrn" .
                        "Cookie: ab-leave-review-header-cta-jRyMvnA2RtyN0xgT_5QFKQ=1; ab-CNVRSN-1274-ExperimentalABTest-CNVRSN-1274=-1; ab-raq-banner-redesign-fGhMi2BERVy9PJfV_fE0Bw=2; ab-search-redesign-AXfJKargTTim4HB5MBMDvA=5; ab-single-step-review-flow-jSgdLXrpQkYz7nHjJTeGNz=1; cat-device-id=e4e4914b-3a45-48c9-a960-a3dd19a574ca; _gid=GA1.2.689050058.1689986612; gaconnector2_id=d45bc0ad-653b-bce3-cb51-8206a09ec8d9; OptanonAlertBoxClosed=2023-07-22T00:43:34.324Z; _gcl_au=1.1.489400752.1689986615; _fbp=fb.1.1689986615265.651102832; _hjSessionUser_1612090=eyJpZCI6IjdlMGUyYWQwLTI2YzEtNTg1YS1hNjFmLTkyNWZmNTZkNTMwYyIsImNyZWF0ZWQiOjE2ODk5ODY2NjY3NTAsImV4aXN0aW5nIjp0cnVlfQ==; visitor_id796483=630847074; visitor_id796483-hash=a4838594bffb94b9f49ebc6ef85db8e2186679dbdd22d0d159f78bf300131126c931784288178d98789e7786fc52a5ab9843834b; cat-session-id=190aa00c-64c5-41da-b369-be39107187b7; _hjIncludedInSessionSample_1612090=1; _hjSession_1612090=eyJpZCI6IjU4ZWRjNjZhLTMyYTMtNDBjYy1iMThmLWRmZGRjZTdjMDJmMSIsImNyZWF0ZWQiOjE2OTAwNjUwMzIyNzEsImluU2FtcGxlIjp0cnVlfQ==; __gtm_CAT_referrer=https%3A%2F%2Fwww.checkatrade.com%2F; _gaexp=GAX1.2.SobaQPWvTpCPSJDIxQDUOA.19652.1; _tt_enable_cookie=1; _ttp=MbzY2Cj5INgs2Ry8HtIIPpzGVJl; LPVID=VkZTdhMzhlNjFmZjM0NDkz; LPSID-12271206=71SW8hqJTWytpUAfr83rYA; __cf_bm=606qxACYiykr.fSmCLGXA0s3RhZErYUF5cZo.fQBmMs-1690067691-0-AUgSYMAOwPP4D6pFHFgxui3ybhbOGogVPU5dfPHg0R8L9JK5fO+oDbwhkg3caO2juH0RQGRNNOdJ89fGFcWNmpPKp8/g7Xg0c1H9SL5svh6/; _hjAbsoluteSessionInProgress=0; OptanonConsent=isGpcEnabled=0&datestamp=Sun+Jul+23+2023+00%3A16%3A16+GMT%2B0100+(British+Summer+Time)&version=6.38.0&isIABGlobal=false&hosts=&consentId=d3dc790c-917a-4f33-9ff7-82398bf33d06&interactionCount=1&landingPath=NotLandingPage&groups=C0003%3A1%2CC0004%3A1%2CC0002%3A1%2CC0001%3A1&geolocation=GB%3BENG&AwaitingReconsent=false; _uetsid=c846f360282811eea2de810e869e241d; _uetvid=c84705b0282811eeb2d531f0bf6cd4e0; _ga_HTBJVBRDH2=GS1.1.1690065032.4.1.1690067777.58.0.0; _ga=GA1.2.1127313496.1689986612rn" .
                        "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36rn"
                
        ]
    ];
    
    // DOCS: https://www.php.net/manual/en/function.stream-context-create.php
    $context = stream_context_create($opts);
        // Open a webpage
       $homepage = file_get_contents('https://www.checkatrade.com', false, $context);
      
       echo htmlspecialchars($homepage);
    
        // Set the filename
        $file = 'hp.txt';
        // Write the contents back to the file
        file_put_contents($file, htmlspecialchars($homepage));
    

  2. Try using file_get_contents with an http:// URL. If it works, it suggests that the problem may be related to your SSL/TLS settings.

    Try opening the URL with curl in PHP. If curl can access the URL but file_get_contents cannot, it would suggest that the problem might be with PHP’s stream settings.

    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'https://www.example.com/api/getData');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $homepage = curl_exec($ch);
    curl_close($ch);
    
    $file = 'sample.txt';
    file_put_contents($file, $homepage);
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search