Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

276
Visualizações
How can I transfer JSON CURL to JSON PHP via API?

I have a templated code for cURL request: create 1 new patient via API

I need transfer it to PHP

curl --location --request POST 'https://www.b2bmd.app/api.php?fn=patient-info' \
--form 'firstname="Test Firstname"' \
--form 'lastname="Test Lastname"' \
--form 'email="abc@gmail.com"' \
--form 'phone="(000) 000-0000"' \
--form 'address1="Address"' \
--form 'qas="{
\"q\": {
\"1\": \"Can you speak?\",
\"2\": \"Can you hear?\"
},
\"a\": {
\"1\": \"Yes\",
\"2\": \"Yes \"
}
}"' \

And my code

 
        $ch = curl_init();
        $myArray = array(
            array('Can you speak?','Can you hear?'),
            array('Yes','Yes')
        );
        $json = json_encode($myArray);
        
        
        /**add new **/
        
        $url = "https://www.b2bmd.app/api.php?fn=patient-info";
         $dataarr = array(
        "firstname" => "John",
        "lastname" => "Mike",
        "email" => "John20124@yahoo.com",
        "phone" => "(781) 921-4790",
        "address1" => "7231 street 4",
        "qas" => $json
        ); 
        
        $data = http_build_query($dataarr);
        
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $resp = curl_exec($ch); 
        if($e = curl_error($ch))
        {
            echo $e;
        }
        else
        {
            $decode = json_decode($resp, true); 
            
            print_r($decode);
            
            
        }
        curl_close($ch);

I can transfer all to php code.

But only qas is JSon style I can't convert to php. I have tried many ways but still get the error "Not valid QAS JSON string." I've tried everything and it still doesn't work.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The first rule of debugging (and programming): Break The Problem Down.

You've identified that the problem is the "qas" parameter, and that it needs to be the right JSON string, so ignore all your code except for the part that makes that string:

$myArray = array(
    array('Can you speak?','Can you hear?'),
    array('Yes','Yes')
);
$json = json_encode($myArray);
echo $json;

This gives:

[["Can you speak?","Can you hear?"],["Yes","Yes"]]

But your successful request had this:

{
    "q": {
        "1": "Can you speak?",
        "2": "Can you hear?"
    },
    "a": {
        "1": "Yes",
        "2": "Yes"
    }
}

So your example is missing the "q" and "a" keys around the questions and answers, and the "1" and "2" keys inside them.

The solution is simply to add those keys in the same places in the PHP array:

$myArray = array(
    "q" => array(1=>'Can you speak?',2=>'Can you hear?'),
    "a" => array(1=>'Yes',2=>'Yes')
);
$json = json_encode($myArray);
echo $json;

Which gives:

{"q":{"1":"Can you speak?","2":"Can you hear?"},"a":{"1":"Yes","2":"Yes"}}

Now that you have JSON you think looks right, you can plug it back into your curl code and see if it works. If not, repeat the process: narrow down the problem, and look directly at the data.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda