PHP code example of snapsearch / snapsearch-client-php
1. Go to this page and download the library: Download snapsearch/snapsearch-client-php library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
snapsearch / snapsearch-client-php example snippets
\SnapSearchClientPHP\Bootstrap::register();
$client = new \SnapSearchClientPHP\Client('email', 'key');
$detector = new \SnapSearchClientPHP\Detector;
$interceptor = new \SnapSearchClientPHP\Interceptor($client, $detector);
//exceptions should be ignored in production, but during development you can check it for validation errors
try{
$response = $interceptor->intercept();
}catch(SnapSearchClientPHP\SnapSearchException $e){}
if($response){
//this request is from a robot
//status code
header(' ', true, $response['status']); //as of PHP 5.4, you can use http_response_code($response['status']);
//the complete $response['headers'] is not returned to the search engine due to potential content or transfer encoding issues, except for the potential location header, which is used when there is an HTTP redirect
if(!empty($response['headers'])){
foreach($response['headers'] as $header){
if($header['name'] == 'Location'){
header($header['name'] . ': ' . $header['value']);
}
}
}
//content
echo $response['html'];
}else{
//this request is not from a robot
//continue with normal operations...
}
$request_parameters = array(
//add your API request parameters if you have any...
);
$blacklisted_routes = array(
//add your black listed routes if you have any...
);
$whitelisted_routes = array(
//add your white listed routes if you have any...
);
$check_file_extensions = //if you wish for SnapSearchClient to check if the URL leads to a static file, switch this on to a boolean true, however this is expensive and time consuming, so it's better to use black listed or white listed routes
$symfony_http_request_object = //get the Symfony\Component\HttpFoundation\Request
$robot_json_path = //if you have a custom robots.json you can choose to use that instead, use the absolute path
$extensions_json_path = //if you have a custom extensions.json you can choose hat insead, use the absolute path
$client = new \SnapSearchClientPHP\Client('email', 'key', $request_parameters);
$detector = new \SnapSearchClientPHP\Detector(
$blacklisted_routes,
$whitelisted_routes,
$check_file_extensions,
$symfony_http_request_object,
$robot_json_path,
$extensions_json_path
);
//robots can be direct accessed and manipulated
$detector->robots['match'][] = 'my_custom_bot_to_be_matched';
$detector->robots['ignore'][] = 'my_ignored_robot';
//extensions can as well, add to 'generic' or 'php'
$detector->extensions['php'][] = 'validextension';
$interceptor = new \SnapSearchClientPHP\Interceptor($client, $detector);
//your custom cache driver
$cache = new YourCustomClientSideCacheDriver;
//the before_intercept callback is called after the Detector has detected a search engine robot
//if this callback returns an array, the array will be used as the $response to $interceptor->intercept();
//use it for client side caching in order to have millisecond responses to search engines
//the after_intercept callback can be used to store the snapshot from SnapSearch as a client side cached resource
//this is of course optional as SnapSearch caches your snapshot as well!
$interceptor->before_intercept(function($url) use ($cache){
//get cache from redis/filesystem..etc
//returned value should array if successful or boolean false if cache did not exist
return $cache->get($url);
})->after_intercept(function($url, $response) use ($cache){
//the cached time should be less then the cached time you passed to SnapSearch, we recommend half the SnapSearch cachetime
$time = '12hrs';
$cache->store($url, $response, $time);
});
//exceptions should be ignored in production, but during development you can check it for validation errors
try{
$response = $interceptor->intercept();
}catch(SnapSearchClientPHP\SnapSearchException $e){}
if($response){
//this request is from a robot
//status code
header(' ', true, $response['status']); //as of PHP 5.4, you can use http_response_code($response['status']);
//the complete $response['headers'] is not returned to the search engine due to potential content or transfer encoding issues, except for the potential location header, which is used when there is an HTTP redirect
if(!empty($response['headers'])){
foreach($response['headers'] as $header){
if(strtolower($header['name']) == 'location'){
header($header['name'] . ': ' . $header['value']);
}
}
}
//content
echo $response['html'];
}else{
//this request is not from a robot
//continue with normal operations...
}
$app = //HTTP Kernel core controller
$stack = (new \Stack\Builder)->push(
'\SnapSearchClientPHP\StackInterceptor',
new Interceptor(
new Client('email', 'key'),
new Detector
)->before_intercept(function($url){
//before interception callback (optional and chainable)
})->after_intercept(function($url, $response){
//after interception callback (optional and chainable)
}),
function(array $response){
//this callback is completely optional, it allows you to customise your response
//the $response array comes from SnapSearch and contains [(string) 'status', (array) 'headers', (string) 'html']
//remember $response['headers'] is in this format:
//[
// [
// 'name' => 'Location',
// 'value' => 'http://redirect.com/'
// ]
//]
//it's an array of arrays which contain name and value properties
//it's recommended to not pass through all of the headers, due to possible encoding problems
//your server will already output the necessary headers anyway
//however we are passing through the location header if it exists
$headers = array_filter($response['headers'], function($header){
if(strtolower($header['name']) == 'location'){
return true;
}
return false;
});
return [
'status' => $response['status'],
'headers' => $headers,
'html' => $response['html']
];
},
function($exception, $request){
//this is the exception callback and it's completely optional
//it will only be called if a SnapSearchException is raised
//which only happens if SnapSearch's servers are temporarily offline
//if there is an exception, this middleware will simply pass to the next layer
//if you want to stop and inspect or log the actual exception, this is where you can do it
}
);
$app = $stack->resolve($app);
$request = Request::createFromGlobals();
$response = $app->handle($request)->send();
$app->terminate($request, $response);
//or just do this if you have Stack\run
//\Stack\run($app);
"snapsearch/snapsearch-client-php": "~1.2"
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.