Hello Team,
I want to make one module in PHP Language in which i want to integrate Grandstream API for schedule a call using API. Attached herewith my script for your reference. When i execute this script it always gives me output like:
OUTPUT:
XML Request Data for Wakeup Call: 3001 1 1 20230929 1430
API Response from GS: { “response”: { }, “status”: -1 }
In above output if API Response from GS: { “response”: { }, “status”: 0 } then only i can set up schedule call. can you please guide me what is the issue in my code or provide me Response Data format for Schedule/Wakeup Call.
This is my code:
<?php $username = '*******'; $password = '**********'; $apiEndpoint ='https://70.61.67.236:6443/api'; $requestData = json_encode([ 'request' => [ 'action' => 'challenge', 'user' => $username, 'version' => '1.0', ], ]); $ch = curl_init($apiEndpoint); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, [ 'Content-Type: application/json;charset=UTF-8', ]); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $requestData); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Disable SSL verification curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // Disable hostname verification $response = curl_exec($ch); if ($response === false) { die('Error: ' . curl_error($ch)); } curl_close($ch); // Step 2: Extract the challenge string from the response $responseData = json_decode($response, true); if (isset($responseData['response']['challenge'])) { $challenge = $responseData['response']['challenge']; // Step 3: Generate the token based on the challenge and password $token = md5($username . $password); $token1 = md5($challenge . $password); // Step 4: Send a login request with the token $loginRequestData = json_encode([ 'request' => [ 'action' => 'login', 'token' => $token1, 'url' => '', // Provide the URL if required 'user' => $username, ], ]); $ch = curl_init($apiEndpoint); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, [ 'Content-Type: application/json;charset=UTF-8', 'Authorization: Bearer ' . $token1, ]); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $loginRequestData); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); $loginResponse = curl_exec($ch); if ($loginResponse === false) { die('Error: ' . curl_error($ch)); } curl_close($ch); $loginResponseData = json_decode($loginResponse, true); if (isset($loginResponseData['response']['cookie'])) { $cookie = $loginResponseData['response']['cookie']; echo ""; $xmlData = ' 3001 1 1 20230929 1430 '; // Log the XML data for debugging echo "XML Request Data for Wakeup Call:\n"; echo $xmlData; echo "
"; // Encode the XML data $encodedXmlData = htmlspecialchars($xmlData); $requestData1 = json_encode([ "action" => "pmsapi", "token" => $token, // Use the token obtained during login "format" => "xml", "data" => $encodedXmlData, // Use the encoded XML data "cookie" => $cookie, // Use the obtained cookie ]); //echo $requestData1; $ch = curl_init($apiEndpoint); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, [ 'Content-Type: application/json;charset=UTF-8', 'Authorization: Bearer ' . $token, ]); curl_setopt($ch, CURLOPT_VERBOSE, true); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $requestData1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // Execute the cURL request $response = curl_exec($ch); // Check for cURL errors if (curl_errno($ch)) { echo 'Curl error: ' . curl_error($ch); } else { echo "API Response from GS:\n"; echo $response; $jsonResponse = json_decode($response, true); //echo $jsonResponse; if ($jsonResponse !== null) { if (isset($jsonResponse['response'])) { //$jsonData = $jsonResponse['response']; //echo $jsonData; } } else { $xml = simplexml_load_string($response); if ($xml !== false) { echo "hello"; if (isset($xml->wakeup_status)) { $wakeupStatus = (int)$xml->wakeup_status; if ($wakeupStatus === 1) { echo "Wake-up call is set.\n"; } else { echo "Wake-up call is not set.\n"; } } else { echo "Unable to determine wake-up call status from the response.\n"; } } else { } } } // Close cURL session curl_close($ch); } else { echo "Error: Unable to log in."; } } else { echo "Error: Unable to retrieve challenge string."; } ?>