Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

277
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!