API Documentation > Sample Codes > Create Website Screenshot With Callback API

Create Website Screenshot With Callback API


Callback feature allow you send us a lot of requests in a short period, e.g. 1,000 requests in 5 minutes. The RESTFul API with callback require your system check the result in a loop. But the callback feature allow ask us to send a notification when we finish the screenshot of one page. Please see those two images to compare those two modes.

RESTFul API without callback

RESTFul API with callback

As you can see, callback API can save your system a lot of time and action.

How to use it?

It is very simple and straight forward to use callback if you are familiar with facebook callback API. If you do not, you can send us a request with source code you have. We will help you to debug and make sure it works on your server. Of cause, if you still want to do it yourself, please see following information.

1. You need prepare a public URL which can be accessed us. One of our customers asked why the callback does not work on his system. After two rounds back and forth, we find he send us a url like "http://localhost/callback.php". :) We recommend something like this:


Tip: if you want to send us a parameter like a token, you can add it into the callback url. We will pass it back to you.

2. Call the API server.

For example, your system call our API server like this:


Our server will call your system when the screenshot is ready.


	"ori_url": "http://www.google.com"


Why I can receive the same array in the directly response from your server?

It is because the cache. When your request hit the cache, we will send the response to you immediately. And, we will call your callback URL as well to make sure the process is as same as a new request.

How it will charge me?

If the call hit cache, we will charge by rate of hit cache. It only take 0.03 credit when the image is around 20K. If it is a new request, it will charge you 1 API credit. If your provided a wrong callback url, we still will reduce one API credit since system already did the work.

Should I keep the loop as the sample code for no callback RESTFul link?

No, you do not need keep it. Remove the loop please.

How I can prevent someone else access the callback URL?

We recommend you to add a token in the callback URL as we mentioned above. If the token is not right, it means it is somebody else.

Sample code (PHP)

How to send a request?


$api_url "http://api.page2images.com/restfullink";
// URL can be those formats: http://www.google.com https://google.com google.com and www.google.com
    // But free rate plan does not support SSL link.
    // We use google here as an example does not mean we like google or hate google, just because their network is good in most of area in the world.
$url "http://www.google.com/";
$device 0// 0 - iPhone4, 1 - iPhone5, 2 - Android, 3 - WinPhone, 4 - iPad, 5 - Android Pad, 6 - Desktop, 7 - iPhone6, 8 - iPhone6+

    try {
$para = array(
"p2i_url" => $url,
"p2i_key" => $apikey,
"p2i_device" => $device,
"p2i_wait" => 30,
"p2i_callback" => "http://www.t2.page2images.com/callback.php"

// connect page2images server
$ch curl_init();
$response curl_exec($ch);

        if (empty(
$response)) {
// something error
echo "something error";
        } else {
$json_data json_decode($response);

        switch (
$json_data->status) {
// do something to handle error
$loop_flag FALSE;
$json_data->errno " " $json_data->msg;
//if you hit the cache, you will get the response directly. and we will call the callback as well just for making sure the process is same.
echo "<a href='".$json_data->ori_url."'><img src='".$json_data->image_url."'></a>";
// Or you can download the image from our server
"Page2Images is working hard for you.";
"Very strange here. Maybe page2images have updated their API?";
    } catch (
Exception $e) {
// Do whatever you think is right to handle the exception.
echo 'Caught exception: '$e->getMessage(), "\n";

How to handle the result? (PHP)


if (! empty($_POST["result"])) {
$post_data $_POST["result"];
$json_data json_decode($post_data);
    switch (
$json_data->status) {
// Page2Images encounter problem when handle your request.
            // Please handle the error here.
            // You can get error information at $json_data->msg and $json_data->ori_url
// success! let us store the image to your system.
            // You can get the image at $json_data->image_url. You need download it by code.
            // You can get the original url of this request at $json_data->ori_url . Maybe update the status in your db?
//Should be an error if you receive something else. We will not send processing status to you.
} else {
// Do whatever you think is right to handle the exception.

Still have question?

I am sorry to hear that. Please send us a ticket with source code you have. We will help you to debug and make sure it works on your server.

Sample Codes
Download Java sample source code for website screenshot
Download PHP sample source code for website screenshot
Download Python sample source code for website screenshot
Download PHP sample source code for convert HTML to image
Use Code Generator to Build the Code

Using our Code Generator, you can quickly generate the source code for IMG Tags. Use page2images code generator for website screenshot.

Free Engineering Service

Premium and XLarge rate plan users get free help with writing and testing code. Please contact us if you need help.