نمونهٔ کامل
یک نمونهٔ کامل PHP که روند ورود، دریافت سؤال و ثبت پاسخ را نشان میدهد.
dq-client.php
<?php
const DQ_API = 'https://dailyquiz.ir/wp-json/daily-quiz/v1';
const DQ_KEY = 'dqk_your_api_key';
const EXAM = 'exam_AbCdEf123';
function dq_call( $method, $path, $token, $body = null ) {
$args = array(
'method' => $method,
'timeout' => 20,
'headers' => array(
'Authorization' => 'Bearer ' . $token,
'Content-Type' => 'application/json',
),
);
if ( null !== $body ) {
$args['body'] = wp_json_encode( $body );
}
$res = wp_remote_request( DQ_API . $path, $args );
return json_decode( wp_remote_retrieve_body( $res ), true );
}
// 1) Send the code.
$otp = dq_call( 'POST', '/auth/request-otp', DQ_KEY, array( 'phone' => '09123456789', 'exam_token' => EXAM ) );
// 2) After the user types the code:
$session = dq_call( 'POST', '/auth/verify-otp', DQ_KEY, array( 'otp_token' => $otp['otp_token'], 'code' => '48213' ) );
// 3) Questions.
$exam = dq_call( 'GET', '/exams/' . $session['exam_id'] . '/questions', $session['session_token'] );
// 4) Submit answers (option index, zero-based).
$result = dq_call( 'POST', '/exams/' . $session['exam_id'] . '/submit', $session['session_token'], array(
'answers' => array( array( 'question_id' => $exam['questions'][0]['id'], 'answer' => 1 ) ),
) );
echo $result['score'];