Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
/**
* Example of the 121ProData Single Number API check.
* text output.
*/
#Test mode: safe number
$url = 'https://121prodata.co.uk/api/?token=test-token&number=14356487&output=text';
#Test mode: listed number
$url = 'https://121prodata.co.uk/api/?token=test-token&number=27638756&output=text';
#Test mode: error
$url = 'https://121prodata.co.uk/api/?token=test-token&number=76334756&output=text';
try {
list( $httpResponse, $resultText ) = checkNumber( $url );
echo $resultText;
} catch( Exception $e ) {
#catch any exceptions
die( $e->getMessage() );
}
function checkNumber( $url ) {
#check curl exists
if ( ! function_exists( 'curl_version' ) ) {
throw new Exception( 'curl not installed' );
}
#connect to URL given
$ch = curl_init( $url );
if ( $ch === FALSE ) {
throw new Exception( 'Couldnt connect' );
}
#set multiple cURL options
curl_setopt_array(
$ch,
array(
#return the response given by the 121 server
CURLOPT_RETURNTRANSFER => TRUE,
#amount of time to allow the connection to run for
CURLOPT_TIMEOUT_MS => 1000,
#ensures the hostname matches the certificate
CURLOPT_SSL_VERIFYHOST => 2,
/**
* This code will be more secure if the following is set to TRUE however it can
* cause the code to fail.
*
* You may get an error as follows:
* SSL certificate problem, verify that the CA cert is OK.
*/
CURLOPT_SSL_VERIFYPEER => FALSE,
)
);
#execute cURL connection
$result = curl_exec( $ch );
#cURL failed for some reason
if ( $result === FALSE ) {
throw new Exception( 'Curl error: ' . curl_error( $ch ) );
}
#get the HTTP response code
$http_status = curl_getinfo( $ch, CURLINFO_HTTP_CODE );
#return the HTTP response and the result for processing
return array( $http_status, $result );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment