Viewed   72 times

I am using CURL to send a request. The response dataType is json. How can I parse this data and insert it into the database?

<?php

$url = 'http://sms2.cdyne.com/sms.svc/SimpleSMSsendWithPostback?        PhoneNumber=18887477474&Message=test&LicenseKey=LICENSEKEY';

$cURL = curl_init();

curl_setopt($cURL, CURLOPT_URL, $url);
curl_setopt($cURL, CURLOPT_HTTPGET, true);

curl_setopt($cURL, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'Accept: application/json'
));

$result = curl_exec($cURL);

curl_close($cURL);



print_r($result);

?>

JSON Output:

{
    "Cancelled": false,
    "MessageID": "402f481b-c420-481f-b129-7b2d8ce7cf0a",
    "Queued": false,
    "SMSError": 2,
    "SMSIncomingMessages": null,
    "Sent": false,
    "SentDateTime": "/Date(-62135578800000-0500)/"
}

 Answers

3

If your $result variable is a string json like, you must use json_decode function to parse it as an object or array:

$result = '{"Cancelled":false,"MessageID":"402f481b-c420-481f-b129-7b2d8ce7cf0a","Queued":false,"SMSError":2,"SMSIncomingMessages":null,"Sent":false,"SentDateTime":"/Date(-62135578800000-0500)/"}';
$json = json_decode($result, true);
print_r($json);

OUTPUT

Array
(
    [Cancelled] => 
    [MessageID] => 402f481b-c420-481f-b129-7b2d8ce7cf0a
    [Queued] => 
    [SMSError] => 2
    [SMSIncomingMessages] => 
    [Sent] => 
    [SentDateTime] => /Date(-62135578800000-0500)/
)

Now you can work with $json variable as an array:

echo $json['MessageID'];
echo $json['SMSError'];
// other stuff

References:

  • json_decode - PHP Manual
Monday, October 31, 2022
2

Post json objectusing curl.

$data = array('value1' => $value1, 'value2' => $value2);                                                                    
$data_string = json_encode($data);                                                                                   

$ch = curl_init('http://api.local/rest/users');   // where to post                                                                   
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                                                                  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
    'Content-Type: application/json',                                                                                
    'Content-Length: ' . strlen($data_string))                                                                       
);                                                                                                                   

$result = curl_exec($ch);
Saturday, November 19, 2022
 
1

decode your json as array and iterate it as any array as flowing:

$json_decoded= json_decode($json,true);

$tab="t";

foreach ($json_decoded as $key => $val) {
    echo "Article ".$val["key"]."n" ;
    echo $tab."Authors :n";
    foreach ($val["data"]["authors"] as $key => $author){
        echo $tab.$tab. ($key+1) ." - ".$author["firstName"]. " ".$author["lastName"]."n";
    } 

    echo $tab."Article Title: ".$val["data"]["articleTitle"] ."n";
    echo $tab."Publication Title: ".$val["data"]["pubTitle"] ."n";
    echo $tab."Key: ".$val["key"]."n";
}

run on codepad

and you can use the same method for xml as flowing:

$xml = simplexml_load_string($xmlstring);
$json = json_encode($xml);
$json_decoded = json_decode($json,TRUE);
//the rest is same

for xml you can use the SimpleXml's functions or DOMDocument class


Tip

to know the structure of your data that api return to you after it converted to array use var_dump($your_decoded_json) in debuging

Sunday, October 30, 2022
 
2

If you wanna use Content-type: application/json and raw data, seem your data should be in json format

$ch = curl_init();
$headers  = [
            'x-api-key: XXXXXX',
            'Content-Type: text/plain'
        ];
$postData = [
    'data1' => 'value1',
    'data2' => 'value2'
];
curl_setopt($ch, CURLOPT_URL,"XXXXXX");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postData));           
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result     = curl_exec ($ch);
$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
Wednesday, October 12, 2022
 
deeksy
 
4

You can create a gradle task like this

gradle myTask{
 doLast{
  def inputFile = new File("xyz.json")
  def json = new JsonSlurper().parseText(inputFile.text)
  def labels = json.properties.foo.type //This will return "pqr"
 }
}
Tuesday, September 20, 2022
 
blearn
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :