_/
public function refresh_client() {
$post_data = json_encode(array(
'clientContext' => $this->client_context,
'serverContext' => $this->server_context
));
foreach (json_decode($this->make_request('refreshClient', $postdata))->content as $id => $device) {
$this->devices[$id] = $device;
}
}
/*
Play Sound
_/
public function play_sound($device_id, $subject = 'Find My iPhone Alert') {
if(!is_string($device_id)) throw new FindMyiPhoneException('Expected $device_id to be a string');
if(!is_string($subject)) throw new FindMyiPhoneException('Expected $subject to be a string');
$post_data = json_encode(array(
'clientContext' => $this->client_context,
'serverContext' => $this->server_context,
'device' => $device_id,
'subject' => $subject
));
return json_decode($this->make_request('playSound', $postdata))->content[0]->snd;
}
/*
Send Message
_/
public function send_message($device_id, $text, $sound = false, $subject = 'Important Message') {
if(!is_string($device_id)) throw new FindMyiPhoneException('Expected $device_id to be a string');
if(!is_string($text)) throw new FindMyiPhoneException('Expected $text to be a string');
if(!is_bool($sound)) throw new FindMyiPhoneException('Expected $sound to be a bool');
if(!is_string($subject)) throw new FindMyiPhoneException('Expected $subject to be a string');
$post_data = json_encode(array(
'clientContext' => $this->client_context,
'serverContext' => $this->server_context,
'device' => $device_id,
'emailUpdates' => $this->email_updates,
'sound' => $sound,
'subject' => $subject,
'text' => $text,
'userText' => true
));
return json_decode($this->make_request('sendMessage', $postdata))->content[0]->msg;
}
/*
Lock Device
_/
public function lost_device($device_id, $passcode, $owner_phone_number = '911', $sound = true, $text = 'This iPhone has been lost. Please call me.') {
if(!is_string($device_id)) throw new FindMyiPhoneException('Expected $device_id to be a string');
if(!is_string($passcode)) throw new FindMyiPhoneException('Expected $passcode to be a string');
if(strlen($passcode) !== 4) throw new FindMyiPhoneException('Expected $passcode to be 4 characters long');
if(!is_string($owner_phone_number)) throw new FindMyiPhoneException('Expected $owner_phone_number to be a string');
if(!is_bool($sound)) throw new FindMyiPhoneException('Expected $sound to be a bool');
if(!is_string($text)) throw new FindMyiPhoneException('Expected $text to be a string');
$post_data = json_encode(array(
'clientContext' => $this->client_context,
'serverContext' => $this->server_context,
'device' => $device_id,
'emailUpdates' => $this->email_updates,
'lostModeEnabled' => true,
'ownerNbr' => $owner_phone_number,
'passcode' => $passcode,
'sound' => $sound,
'text' => $text,
'trackingEnabled' => true,
'userText' => true
));
return json_decode($this->make_request('lostDevice', $postdata))->content[0]->lostDevice;
}
/*
Notify When Found
_/
public function notify_when_found($device_id, $notify = true) {
if(!is_string($device_id)) throw new FindMyiPhoneException('Expected $device_id to be a string');
if(!is_string($notify)) throw new FindMyiPhoneException('Expected $notify to be a boolean');
$post_data = json_encode(array(
'clientContext' => $this->client_context,
'serverContext' => $this->server_context,
'device' => $device_id,
'lostModeEnabled' => $notify
));
return json_decode($this->make_request('saveLocFoundPref', $postdata))->content[0]->locFoundEnabled;
}
/*
Lock and Message
_/
public function lock_and_message($device_id, $passcode, $text, $sound = true, $title = 'Find My iPhone Alert') {
if(!is_string($device_id)) throw new FindMyiPhoneException('Expected $device_id to be a string');
if(!is_string($passcode)) throw new FindMyiPhoneException('Expected $passcode to be a string');
if(strlen($passcode) !== 4) throw new FindMyiPhoneException('Expected $passcode to be 4 characters long');
if(!is_string($text)) throw new FindMyiPhoneException('Expected $text to be a string');
if(!is_bool($sound)) throw new FindMyiPhoneException('Expected $sound to be a bool');
if(!is_string($title)) throw new FindMyiPhoneException('Expected $title to be a string');
$post_data = json_encode(array(
'clientContext' => $this->client_context,
'serverContext' => $this->server_context,
'device' => $device_id,
'emailUpdates' => $this->email_updates,
'passcode' => $passcode,
'sound' => $sound,
'text' => $text,
'title' => $title,
'userText' => true
));
return json_decode($this->make_request('lockAndMessage', $postdata))->content[0]->remoteLock;
}
/*
Remote Lock
_/
public function remote_lock($device_id, $passcode) {
if(!is_string($device_id)) throw new FindMyiPhoneException('Expected $device_id to be a string');
if(!is_string($passcode)) throw new FindMyiPhoneException('Expected $passcode to be a string');
if(strlen($passcode) !== 4) throw new FindMyiPhoneException('Expected $passcode to be 4 characters long');
$post_data = json_encode(array(
'clientContext' => $this->client_context,
'serverContext' => $this->server_context,
'device' => $device_id,
'emailUpdates' => $this->email_updates,
'passcode' => $passcode
));
return json_decode($this->make_request('remoteLock', $postdata))->content[0]->remoteLock;
}
/*
Remote Wipe
_/
public function remote_wipe($device_id, $passcode, $text) {
if(!is_string($device_id)) throw new FindMyiPhoneException('Expected $device_id to be a string');
if(!is_string($passcode)) throw new FindMyiPhoneException('Expected $passcode to be a string');
if(strlen($passcode) !== 4) throw new FindMyiPhoneException('Expected $passcode to be 4 characters long');
if(!is_string($text)) throw new FindMyiPhoneException('Expected $text to be a string');
$post_data = json_encode(array(
'clientContext' => $this->client_context,
'serverContext' => $this->server_context,
'device' => $device_id,
'passcode' => $passcode,
'text' => $text,
'emailUpdates' => $this->email_updates
));
return json_decode($this->make_request('remoteWipe', $postdata))->content[0]->remoteWipe;
}
/*
Locate Device
_/
public function locate_device($device, $timeout = 120) {
if(!is_integer($device)) throw new FindMyiPhoneException('Expected $device to be an integer');
if(!isset($this->devices[$device])) $this->refresh_client();
$start = time();
while (!$this->devices[$device]->location->locationFinished) {
if ((time() - $start) > intval($timeout)) {
throw new FindMyiPhoneException('Failed to locate device! Request timed out.');
}
sleep(5);
$this->refreshclient();
}
return $this->devices[$device]->location;
}
/*
Make request to the Find My iPhone server.
@param $method - the method
@param $post_data - the POST data
@param $return_headers - also return headers when true
@param $headers - optional headers to send
@return HTTP response
_/
private function make_request($method, $post_data, $return_headers = false, $headers = array()) {
if(!is_string($method)) throw new FindMyiPhoneException('Expected $method to be a string');
if(!$this->is_json($post_data)) throw new FindMyiPhoneException('Expected $post_data to be json');
if(!is_array($headers)) throw new FindMyiPhoneException('Expected $headers to be an array');
if(!is_bool($return_headers)) throw new FindMyiPhoneException('Expected $return_headers to be a bool');
if(!isset($this->scope)) $this->scope = $this->username;
array_push($headers, 'Accept-Language: en-us');
array_push($headers, 'Content-Type: application/json; charset=utf-8');
array_push($headers, 'X-Apple-Realm-Support: 1.0');
array_push($headers, 'X-Apple-Find-Api-Ver: 3.0');
array_push($headers, 'X-Apple-Authscheme: UserIdGuest');
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_TIMEOUT => 9,
CURLOPT_CONNECTTIMEOUT => 5,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_AUTOREFERER => true,
CURLOPT_VERBOSE => false,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $post_data,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_HEADER => $return_headers,
CURLOPT_URL => sprintf("https://%s/fmipservice/device/%s/%s", $this->host, $this->scope, $method),
CURLOPT_USERPWD => $this->username . ':' . $this->password,
CURLOPT_USERAGENT => 'FindMyiPhone/294 CFNetwork/655 Darwin/14.0.0'
));
$http_result = curl_exec($curl);
curl_close($curl);
return $httpresult;
}
/*
Parse cURL headers
@param $response - cURL response including the headers
<?php /**
_/ private function init_client() { $post_data = json_encode(array( 'clientContext' => $this->client_context )); $headers = $this->parse_curl_headers($this->make_request('initClient', $post_data, true)); $this->host = $headers['X-Apple-MMe-Host']; $this->scope = $headers['X-Apple-MMe-Scope']; $this->refreshclient(); } /*
_/ public function refresh_client() { $post_data = json_encode(array( 'clientContext' => $this->client_context, 'serverContext' => $this->server_context )); foreach (json_decode($this->make_request('refreshClient', $postdata))->content as $id => $device) { $this->devices[$id] = $device; } } /*
_/ public function play_sound($device_id, $subject = 'Find My iPhone Alert') { if(!is_string($device_id)) throw new FindMyiPhoneException('Expected $device_id to be a string'); if(!is_string($subject)) throw new FindMyiPhoneException('Expected $subject to be a string'); $post_data = json_encode(array( 'clientContext' => $this->client_context, 'serverContext' => $this->server_context, 'device' => $device_id, 'subject' => $subject )); return json_decode($this->make_request('playSound', $postdata))->content[0]->snd; } /*
_/ public function send_message($device_id, $text, $sound = false, $subject = 'Important Message') { if(!is_string($device_id)) throw new FindMyiPhoneException('Expected $device_id to be a string'); if(!is_string($text)) throw new FindMyiPhoneException('Expected $text to be a string'); if(!is_bool($sound)) throw new FindMyiPhoneException('Expected $sound to be a bool'); if(!is_string($subject)) throw new FindMyiPhoneException('Expected $subject to be a string'); $post_data = json_encode(array( 'clientContext' => $this->client_context, 'serverContext' => $this->server_context, 'device' => $device_id, 'emailUpdates' => $this->email_updates, 'sound' => $sound, 'subject' => $subject, 'text' => $text, 'userText' => true )); return json_decode($this->make_request('sendMessage', $postdata))->content[0]->msg; } /*
_/ public function lost_device($device_id, $passcode, $owner_phone_number = '911', $sound = true, $text = 'This iPhone has been lost. Please call me.') { if(!is_string($device_id)) throw new FindMyiPhoneException('Expected $device_id to be a string'); if(!is_string($passcode)) throw new FindMyiPhoneException('Expected $passcode to be a string'); if(strlen($passcode) !== 4) throw new FindMyiPhoneException('Expected $passcode to be 4 characters long'); if(!is_string($owner_phone_number)) throw new FindMyiPhoneException('Expected $owner_phone_number to be a string'); if(!is_bool($sound)) throw new FindMyiPhoneException('Expected $sound to be a bool'); if(!is_string($text)) throw new FindMyiPhoneException('Expected $text to be a string'); $post_data = json_encode(array( 'clientContext' => $this->client_context, 'serverContext' => $this->server_context, 'device' => $device_id, 'emailUpdates' => $this->email_updates, 'lostModeEnabled' => true, 'ownerNbr' => $owner_phone_number, 'passcode' => $passcode, 'sound' => $sound, 'text' => $text, 'trackingEnabled' => true, 'userText' => true )); return json_decode($this->make_request('lostDevice', $postdata))->content[0]->lostDevice; } /*
_/ public function notify_when_found($device_id, $notify = true) { if(!is_string($device_id)) throw new FindMyiPhoneException('Expected $device_id to be a string'); if(!is_string($notify)) throw new FindMyiPhoneException('Expected $notify to be a boolean'); $post_data = json_encode(array( 'clientContext' => $this->client_context, 'serverContext' => $this->server_context, 'device' => $device_id, 'lostModeEnabled' => $notify )); return json_decode($this->make_request('saveLocFoundPref', $postdata))->content[0]->locFoundEnabled; } /*
_/ public function lock_and_message($device_id, $passcode, $text, $sound = true, $title = 'Find My iPhone Alert') { if(!is_string($device_id)) throw new FindMyiPhoneException('Expected $device_id to be a string'); if(!is_string($passcode)) throw new FindMyiPhoneException('Expected $passcode to be a string'); if(strlen($passcode) !== 4) throw new FindMyiPhoneException('Expected $passcode to be 4 characters long'); if(!is_string($text)) throw new FindMyiPhoneException('Expected $text to be a string'); if(!is_bool($sound)) throw new FindMyiPhoneException('Expected $sound to be a bool'); if(!is_string($title)) throw new FindMyiPhoneException('Expected $title to be a string'); $post_data = json_encode(array( 'clientContext' => $this->client_context, 'serverContext' => $this->server_context, 'device' => $device_id, 'emailUpdates' => $this->email_updates, 'passcode' => $passcode, 'sound' => $sound, 'text' => $text, 'title' => $title, 'userText' => true )); return json_decode($this->make_request('lockAndMessage', $postdata))->content[0]->remoteLock; } /*
_/ public function remote_lock($device_id, $passcode) { if(!is_string($device_id)) throw new FindMyiPhoneException('Expected $device_id to be a string'); if(!is_string($passcode)) throw new FindMyiPhoneException('Expected $passcode to be a string'); if(strlen($passcode) !== 4) throw new FindMyiPhoneException('Expected $passcode to be 4 characters long'); $post_data = json_encode(array( 'clientContext' => $this->client_context, 'serverContext' => $this->server_context, 'device' => $device_id, 'emailUpdates' => $this->email_updates, 'passcode' => $passcode )); return json_decode($this->make_request('remoteLock', $postdata))->content[0]->remoteLock; } /*
_/ public function remote_wipe($device_id, $passcode, $text) { if(!is_string($device_id)) throw new FindMyiPhoneException('Expected $device_id to be a string'); if(!is_string($passcode)) throw new FindMyiPhoneException('Expected $passcode to be a string'); if(strlen($passcode) !== 4) throw new FindMyiPhoneException('Expected $passcode to be 4 characters long'); if(!is_string($text)) throw new FindMyiPhoneException('Expected $text to be a string'); $post_data = json_encode(array( 'clientContext' => $this->client_context, 'serverContext' => $this->server_context, 'device' => $device_id, 'passcode' => $passcode, 'text' => $text, 'emailUpdates' => $this->email_updates )); return json_decode($this->make_request('remoteWipe', $postdata))->content[0]->remoteWipe; } /*
_/ public function locate_device($device, $timeout = 120) { if(!is_integer($device)) throw new FindMyiPhoneException('Expected $device to be an integer'); if(!isset($this->devices[$device])) $this->refresh_client(); $start = time(); while (!$this->devices[$device]->location->locationFinished) { if ((time() - $start) > intval($timeout)) { throw new FindMyiPhoneException('Failed to locate device! Request timed out.'); } sleep(5); $this->refreshclient(); } return $this->devices[$device]->location; } /*