phpscreenshotfile-get-contents

I'm using the screendot API, but the screenshot is not output


php

<?php

$opts = [
    "http" => [
        "method" => "GET",
        "header" => "Authorization: Describe api here"
    ]
];

$context = stream_context_create($opts);

$screenshot = file_get_contents("https://screendot.io/api/standard?url=https://www.google.com", false, $context);

echo $screenshot

?>

I am trying to take a screenshot using the screendot API, but the screenshot is not output. I'm using the sample code as is, but it won't take. Please help me.


Solution

  • Try below code. it will work for you.

    $apiKey = '*********';
    
    $url = "https://screendot.io/api/standard?url=https://www.test.com";
    
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_HTTPHEADER, ["Authorization: Bearer $apiKey"]);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    
    $screenshot = curl_exec($ch);
    
    if ($screenshot === false) {
        echo "cURL Error: " . curl_error($ch);
    } else {
        header("Content-Type: image/png");
        echo $screenshot;
    }
    
    curl_close($ch);
    

    If you'll face same issue give me the error log in comment.