Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

274
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda