0

I come humbly before great developers with this issue of mine. Sending a post request in JSON format to Firebase. Here's my code

$token = $_SESSION['token'];
$data = array(
    'id' => 156,
    'quizName' => $quizName,
    'numberOfQuestions' => $numberOfQuestions, 
    'isTimed' => true,                         
    'numberOfMinutesToComplete' => $numberOfMinutesToComplete,
    'targetedClass' => 'Js One',
    'subject' => $subject_name,
    'schoolLevel' => $schoolLevel,
    'questions' => array('question' => $question, 'questionMark' => $marks, 
        'options' => array('option' => 'A', 'answer'=> $optionA, 'option' => 'B', 'answer'=> $optionB, 'option' => 'C', 'answer' => $optionC, 'option' => 'D', 'answer' => $optionD, 'option' => 'E', 'answer' => $optionE),
        'hasImage' => true,
        'images' => array('images-1' => 'image-1', 'images-2' => 'image-2'),
        'correctionOption' => $correct_option
    ),
    'totalValidMarks' => $totalValidMarks,
    'validUntil' => $validUntil
);

// API URL
$url = ' ';
// Create a new cURL resource
$ch = curl_init( $url );
# Setup request to send json via POST.
$payload = json_encode( array( "customer"=> $data ) );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $payload );
curl_setopt( $ch, CURLOPT_HTTPHEADER, array('x-access-token:'.$token, 'Content-Type:application/json'));
# Return response instead of printing.
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
# Send request.
$result = curl_exec($ch);
curl_close($ch);
# Print response.

echo "<pre>$result.</pre>";

But I'm receiving an error:

"details":[{"message":"\"id\" is required","path":["id"],"type":"any.required","context":{"label":"id","key":"id"}}]}

4
  • Can you please link to the API documentation? Is there no PHP SDK that you could be using instead of cURL? Commented Apr 29, 2020 at 23:23
  • which SDK do you suggest pls? Commented Apr 30, 2020 at 0:54
  • That depends on which Firebase service you're trying to use which is why I asked for a link to the documentation Commented Apr 30, 2020 at 0:55
  • Sorry I didnt get that in time. That's the API body Commented Apr 30, 2020 at 17:15

2 Answers 2

2

Please try sending like this:

    $params = $myDataArray;
    $data_string = json_encode($params);
    $curl = curl_init();
    curl_setopt_array($curl, array(
        CURLOPT_URL => $toEndPoint,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => "",
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 30,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => "POST",
        CURLOPT_POSTFIELDS => $data_string,
        CURLOPT_HTTPHEADER => array(
            "x-access-token: $token",
            "cache-control: no-cache",
            "content-type: application/json",
        ),
    ));

    $response = curl_exec($curl);
    $err = curl_error($curl);
    curl_close($curl);
    if ($err) {
        echo "cURL Error #:" . $err;
    } else {
        echo $response;
    }
    exit;
Sign up to request clarification or add additional context in comments.

10 Comments

Thanks, no more errors but the data is not posting into firebase. how can I add content-length header?
If you are posting data curl with CURLOPT_POSTFIELDS. Then curl will automatically add the length of the data with the header. **this is mentioned in here when CURLOPT_HTTPHEADER need Content-Length
It's still not posting. Do you think there's a problem with my $data array?
Its actually throwing an error : questions must be an array
Did you try to post this data to a url you host to validate if it is really posting or not? in my end point everything works fine
|
0

@godlovesme, to answer for your latest comment:

I don't really like using While loops as they can easily let you fall in an endless loop, but you get what you ask for:

$i = 0;
$questionsArrFormatted = [];
while ($i < count($questionsArr)) {
    $hasImage = count($questionsArr[$i]['question']) ? true : false;
    $questionsArrFormatted[$i] = [
        'question' => $questionsArr[$i]['question'],
        'questionMark' => $questionsArr[$i]['marks'],
        'options' => $questionsArr[$i]['options'], // options should be an array formatted like in your question
        'hasImage' => $hasImage,
        'images' => $questionsArr[$i]['images'],
        'correctionOption' => $questionsArr[$i]['correct_answer']
    ];
    $i++;
}

$data = array(
    'id' => 156,
    'quizName' => $quizName,
    'numberOfQuestions' => $numberOfQuestions,
    'isTimed' => true,
    'numberOfMinutesToComplete' => $numberOfMinutesToComplete,
    'targetedClass' => 'Js One',
    'subject' => $subject_name,
    'schoolLevel' => $schoolLevel,
    'questions' => $questionsArrFormatted,
    'totalValidMarks' => $totalValidMarks,
    'validUntil' => $validUntil
);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.