Skip to content

Instantly share code, notes, and snippets.

@enishant
Created June 29, 2022 07:55
Show Gist options
  • Save enishant/f80ab379c5ace81ee4a077acc5cc6185 to your computer and use it in GitHub Desktop.
Save enishant/f80ab379c5ace81ee4a077acc5cc6185 to your computer and use it in GitHub Desktop.
<?php
function ngrok_fetch($request='',$apiKey='',$method='GET', $postdata=array())
{
$headers = [
'authorization: Bearer ' . $apiKey,
'ngrok-version: 2'
];
if(empty($postdata) === false)
{
$postdata = http_build_query($vars);
}
// $apiUrl = 'https://api.ngrok.com/' . $request;
$apiUrl = 'http://localhost:4040/api/' . $request;
$ch = curl_init();
if(empty($postdata) === false)
{
if($method === 'POST')
{
curl_setopt($ch, CURLOPT_URL, $apiUrl);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
}
else
{
curl_setopt($ch, CURLOPT_URL, $apiUrl . "?" . $postdata);
curl_setopt($ch, CURLOPT_POST, 0);
}
}
else
{
curl_setopt($ch, CURLOPT_URL, $apiUrl);
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$server_output = curl_exec ($ch);
curl_close ($ch);
return json_decode($server_output);
}
$apiKey = 'NGROK_TOKEN_HERE'; # Required to access live api (api.ngrok.com)
$response = ngrok_fetch('tunnels', $apiKey);
if(isset($response->tunnels) && is_array($response->tunnels) && count($response->tunnels) > 0)
{
foreach($response->tunnels as $tunnel)
{
if($tunnel->name === 'your_tunnel_name' && $tunnel->proto === 'https')
{
echo $tunnel->public_url;
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment