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.
As you can see, callback API can save your system a lot of time and action.
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:
http://www.yourdomain.com/p2i_callback.php
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:
http://api.page2images.com/restfullink?p2i_url=http://www.google.com&p2i_device=6&p2i_key=YOUR_P2I_KEY&p2i_callback=http://www.yourdomain.com/p2i_callback.php?token=u823sd1
Our server will call your system when the screenshot is ready.
http://www.yourdomain.com/p2i_callback.php?token=u823sd1
{ "status":"finished", "image_url":"http://img.page2images.com/images/00/00/10923423.jpg", "duration":"11", "left_calls":"923023", "ori_url": "http://www.google.com" }
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.
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.
No, you do not need keep it. Remove the loop please.
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.
<?php
$apikey = "YOUR_API_KEY";
$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+
set_time_limit(120);
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();
curl_setopt($ch, CURLOPT_URL, $api_url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($para));
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
$response = curl_exec($ch);
curl_close($ch);
if (empty($response)) {
// something error
echo "something error";
break;
} else {
$json_data = json_decode($response);
}
switch ($json_data->status) {
case "error":
// do something to handle error
$loop_flag = FALSE;
echo $json_data->errno . " " . $json_data->msg;
break;
case "finished":
//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
break;
case "processing":
echo "Page2Images is working hard for you.";
break;
default:
echo "Very strange here. Maybe page2images have updated their API?";
break;
}
} catch (Exception $e) {
// Do whatever you think is right to handle the exception.
echo 'Caught exception: ', $e->getMessage(), "\n";
}
<?php
if (! empty($_POST["result"])) {
$post_data = $_POST["result"];
$json_data = json_decode($post_data);
switch ($json_data->status) {
case "error":
// 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
break;
case "finished":
// 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?
break;
default:
//Should be an error if you receive something else. We will not send processing status to you.
break;
}
} else {
// Do whatever you think is right to handle the exception.
}
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.
Using our Code Generator, you can quickly generate the source code for IMG Tags. Use page2images code generator for website screenshot.
Premium and XLarge rate plan users get free help with writing and testing code. Please contact us if you need help.