3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php // API access key from Google API's Console define( 'API_ACCESS_KEY', 'AIzaSyBNVpyGHobo0YbdyOejbL7ixMES9iwV6VA' ); $registrationIds = array("APA91bHgZ3qu3-VeJ0RC0J9EyB33qX5jB4pX1qEiZtdEiYSJoF8gYA67AeL0j4g7z6sNwLHg1kIQ__p5foZhWnXBybhcEZjp650DzUZXqQBf_fZnWc8xqVIMQMUrdHOtzYDX7DsyL7fyFFoUsF9pElZh8AYftELjlGbJqs-mGZSYg6zBVgXjhq0"); // prep the bundle $msg = array ( 'message' => 'Hello Prem here. Did you get this. message', 'title' => 'This is a title. title', 'subtitle' => 'This is a subtitle. subtitle', 'tickerText' => 'Ticker text here...Ticker text here...Ticker text here', 'vibrate' => 1, 'sound' => 1 ); $fields = array ( 'registration_ids' => $registrationIds, 'data' => $msg ); $headers = array ( 'Authorization: key=' . API_ACCESS_KEY//, //'Content-Type: application/json' ); // Submit those variables to the server $post_data = array( 'test' => 'foobar', 'okay' => 'yes', 'number' => 2 ); // Send a request to example.com $result = post_request('https://android.googleapis.com/gcm/send', $fields, $headers); //$ch = curl_init(); //curl_setopt( $ch,CURLOPT_URL, 'https://android.googleapis.com/gcm/send' ); //curl_setopt( $ch,CURLOPT_POST, true ); //curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers ); //curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true ); //curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false ); //curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) ); //$result = curl_exec($ch ); //curl_close( $ch ); echo $result; function post_request($url, $data, $headers, $referer='') { // Convert the data array into URL Parameters like a=b&foo=bar etc. $data = http_build_query($data); // parse the given URL $url = parse_url($url); if ($url['scheme'] != 'http') { die('Error: Only HTTP request are supported !'); } // extract host and path: $host = $url['host']; $path = $url['path']; // open a socket connection on port 80 - timeout: 30 sec $fp = fsockopen($host, 80, $errno, $errstr, 30); if ($fp){ // send the request headers: fputs($fp, "POST $path HTTP/1.1\r\n"); fputs($fp, "Host: $host\r\n"); if ($referer != '') fputs($fp, "Referer: $referer\r\n"); fputs($fp, $headers); fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n"); fputs($fp, "Content-length: ". strlen($data) ."\r\n"); fputs($fp, "Connection: close\r\n\r\n"); fputs($fp, $data); $result = ''; while(!feof($fp)) { // receive the results of the request $result .= fgets($fp, 128); } } else { return array( 'status' => 'err', 'error' => "$errstr ($errno)" ); } // close the socket connection: fclose($fp); // split the result header from the content $result = explode("\r\n\r\n", $result, 2); $header = isset($result[0]) ? $result[0] : ''; $content = isset($result[1]) ? $result[1] : ''; // return as structured array: return array( 'status' => 'ok', 'header' => $header, 'content' => $content ); } ?>
Output for 5.0.0 - 5.6.21, hhvm-3.10.0 - 3.12.0, 7.0.0 - 7.1.0
Error: Only HTTP request are supported !
Output for 4.4.5 - 4.4.9
Fatal error: Call to undefined function: http_build_query() in /in/gIsag on line 62
Process exited with code 255.
Output for 4.3.2 - 4.4.4
Fatal error: Call to undefined function: http_build_query() in /in/gIsag on line 62
Process exited with code 255.
Output for 4.3.0 - 4.3.1
Fatal error: Call to undefined function: http_build_query() in /in/gIsag on line 62