Last active
December 14, 2015 07:19
-
-
Save NoiseEee/5050272 to your computer and use it in GitHub Desktop.
PHP talks to Node
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* An attempt to have PHP send shit to Node, so that it can emit to our connected JS clients | |
* | |
*/ | |
class nodeNotifier { | |
public $dataToSend = array(); | |
public $nodeServerPort = 8000; | |
public $broadcastNamespace; | |
public $appName; | |
/** | |
* @return bool | |
* @throws Exception | |
*/ | |
function notifyNode() { | |
if(array_key_exists('SERVER_SOFTWARE',$_SERVER) && preg_match("/win/i",$_SERVER['SERVER_SOFTWARE'])) { | |
$OS = "windows";//not running node on development machines using windows | |
return true; | |
} | |
if(empty($this->dataToSend) || !is_array($this->dataToSend)) { | |
throw new Exception("Nothing to notify Node about! No data set"); | |
} | |
//$this->dataToSend['broadcastNamespace'] = $this->broadcastNamespace; | |
$data['broadcastNamespace'] = $this->broadcastNamespace; | |
$data['data'] = json_encode($this->dataToSend); | |
$binSource = "http://127.0.0.1/".$this->appName; | |
$ch = curl_init(); | |
curl_setopt($ch,CURLOPT_URL,$binSource); | |
curl_setopt($ch,CURLOPT_HTTPHEADER,array("Expect:")); | |
curl_setopt($ch,CURLOPT_PORT,$this->nodeServerPort); | |
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,2); | |
curl_setopt($ch,CURLOPT_POST,true); | |
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, 0); | |
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST, 0); | |
curl_setopt($ch,CURLOPT_POSTFIELDS,$data); | |
if(curl_exec($ch)===FALSE) { | |
throw new Exception("CURL error; did not exec()".curl_error($ch)); | |
}; | |
curl_close($ch); | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment