Tboozee Posted March 27, 2017 Share Posted March 27, 2017 Good Day Friends, I am so new to php in general and the system I had to do requires integration with other external system, I have made use of the Web Service which I developed in PhP, all works fine. Now my problem is to actually be able to read the array when consuming the results from the web service that runs from the other company. I am able to print out the result from this statement below: $curl = curl_init(); $accrdNo = 'BVA020'; curl_setopt ($curl, CURLOPT_URL, "[url=]http://thecompany's[/url] url/index.php?id=$accrdNo"); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_PROXY, '111.11.10.81'); curl_setopt($curl, CURLOPT_PROXYPORT, '8080'); $result = curl_exec ($curl); curl_close ($curl); print $result; Here below are the results: {"CODE":"01","MESSAGE":"Certificate Found"} {"data":[{"lab_id":"9","lab_no":"PPP018","lab_parent":"BVA018","lab_name":"Molanganiii (Pty) Ltd","comp_reg_no":"","lab_description":"CODES OF GOOD PRACTICE","disciplines":"The facility complies with the requirements","phys_1":"Mile Road Park","phys_2":"Ruwanda","phys_city":"Mali","phys_code":"9999","province":"Mule","updated":"2011-01-25 16:01:45"}]} Now, I need a php code that will help me to only display from the first line, the CODE's value and Message's value. Again, in the next line, I need to display fields such as lab_id, lab_no and lab_name. How do I write such code, Please help...? Quote Link to comment Share on other sites More sharing options...
requinix Posted March 27, 2017 Share Posted March 27, 2017 That is JSON. PHP has ways of dealing with it. Quote Link to comment Share on other sites More sharing options...
Tboozee Posted March 27, 2017 Author Share Posted March 27, 2017 Well, thank but I checked the examples but I still can'f able to read through those results, may you please write me some examples if you can, Please...I treid my level best and I failed. Please educate me. Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted March 27, 2017 Share Posted March 27, 2017 If the 20 examples in the manual don't help you, then a 21st example isn't going to help you either. So what have you tried? Show the code and tell us what the problem is. Quote Link to comment Share on other sites More sharing options...
gizmola Posted March 27, 2017 Share Posted March 27, 2017 The non-obvious issue is that your string is not valid json. If you utilize echo json_last_error(); You'll see that you are getting an error after json_decode. This is because you have 2 json strings being returned, not one, and this will cause an error. This either needs to be fixed on the server side, or you need to work around the fact that you are getting an invalid response. If you hardwire this string into your script it should help you see what would be valid json: $json = '[{"CODE":"01","MESSAGE":"Certificate Found"}, {"data":[{"lab_id":"9","lab_no":"PPP018","lab_parent":"BVA018","lab_name":"Molanganiii (Pty) Ltd","comp_reg_no":"","lab_description":"CODES OF GOOD PRACTICE","disciplines":"The facility complies with the ments","phys_1":"Mile Road Park","phys_2":"Ruwanda","phys_city":"Mali","phys_code":"9999","province":"Mule","updated":"2011-01-25 16:01:45"}]}]'; $decoded = json_decode($json, true); echo $decoded; Quote Link to comment Share on other sites More sharing options...
Tboozee Posted March 29, 2017 Author Share Posted March 29, 2017 Thanks Gizmola for your response, like I mentioned that I am new to this Web Service and unfortunately I had to do this urgently. From your explaination I started thinking that perhaps the problem is with the JSON Results from the web service I am trying to consume this from, Now, perhaps if I post the the results as they are and the code I am trying to display the fields from these results, perhaps you guys may be able to help me because I struggled with this the whole of yesterday. Here below are the JSON Results followed by the actual code to display fields: JSON Results: =========== {"CODE":"01","MESSAGE":"Certificate Found"}{"data":[{"lab_id":"5","lab_no":"BVA020","lab_parent":"BVA020","lab_name":"National Empowerment Rating Agency (Pty) Ltd","comp_reg_no":"","lab_description":"VERIFICATION OF B-BBEE CODES OF GOOD PRACTICE","disciplines":"The facility complies with the requirements of SANAS R 47-02 ","phys_1":"Corner Main Office Park, Building A\r\n2 Paine Road","phys_2":"Bryanston","phys_city":" ","phys_code":"2191","province":"Gauteng","post_1":"P O Box 130188","post_2":null,"post_city":"Bryanston","post_code":"2021","tel_1":"(011) 463-8900","tel_2":"082 467 0913","fax":"(086) 729-2894","web_url":null,"lab_head":null,"lab_hd_email":null,"lab_contact":"Mr E Mohamed","contact_email":"ebrahiem@nerasa.co.za","accredited":"Y","accreditation_status":"Accredited","start_date":"0000-00-00","expiry_date":"2019-02-05","file_link":"BVA020-06-16","updated":"2016-06-27 08:06:18"}]} Code to display field\s ================== $accrdNo = '3001044'; $url = "http://localhost:8080/webservice/index.php?id=$accrdNo"; $client = curl_init($url); curl_setopt($client, CURLOPT_URL, $url); curl_setopt($client, CURLOPT_CONNECTTIMEOUT, 2); curl_setopt($client, CURLOPT_RETURNTRANSFER, 1); //curl_setopt($c, CURLOPT_HTTPHEADER, array('Accept: application/json', 'Content-Type: application/json')); $result = curl_exec($client); $encoded = json_encode($result, true); //var_dump($encoded); $decoded = json_decode($encoded, JSON_FORCE_OBJECT); echo "<br />"; //var_dump($decoded, true); echo $decoded->'va_entity_name'; These are the clarities I will like to hear from you: 1. Are my JSON results in correct format, if it is, are my results in an array or string format? 2. If the results are not in a correct format, what would be the problem and how I do fix it? 3. How do I resolve these whole mess ? Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted March 29, 2017 Share Posted March 29, 2017 How do I resolve these whole mess ? By approaching the problem systematically instead of using trial-and-error and fantasy functions. Encoding JSON data as JSON is obviously nonsense, the json_encode() function doesn't even have a boolean parameter, nor does json_decode() support JSON_FORCE_OBJECT. So the code isn't going to help you. Actually, forget about PHP for now and just use your browser. When you enter the URL, what do you see on the screen? Quote Link to comment Share on other sites More sharing options...
Tboozee Posted March 29, 2017 Author Share Posted March 29, 2017 These part of the results is what I get from my browser: {"CODE":"01","MESSAGE":"Certificate Found"}{"data":[{"lab_id":"5","lab_no":"BVA020","lab_parent":"BVA020","lab_name":"National Empowerment Rating Agency (Pty) Ltd","comp_reg_no":"","lab_description":"VERIFICATION OF B-BBEE CODES OF GOOD PRACTICE","disciplines":"The facility complies with the requirements of SANAS R 47-02 ","phys_1":"Corner Main Office Park, Building A\r\n2 Paine Road","phys_2":"Bryanston","phys_city":" ","phys_code":"2191","province":"Gauteng","post_1":"P O Box 130188","post_2":null,"post_city":"Bryanston","post_code":"2021","tel_1":"(011) 463-8900","tel_2":"082 467 0913","fax":"(086) 729-2894","web_url":null,"lab_head":null,"lab_hd_email":null,"lab_contact":"Mr E Mohamed","contact_email":"ebrahiem@nerasa.co.za","accredited":"Y","accreditation_status":"Accredited","start_date":"0000-00-00","expiry_date":"2019-02-05","file_link":"BVA020-06-16","updated":"2016-06-27 08:06:18"}]} Quote Link to comment Share on other sites More sharing options...
requinix Posted March 30, 2017 Share Posted March 30, 2017 PHP doesn't have anything built-in that can handle it, but you may be able to take shortcuts to work with it. (A streaming JSON decoder would be the proper solution here but the implementations I see aren't exactly simple or lightweight.) The first object probably doesn't have any {}s embedded in a string, so $result1 = substr($result, 0, strcspn($result, "}") + 1); $object1 = json_decode($result1); //echo $object1->CODE; // 01The main object would be whatever follows that. $result2 = substr($result, strlen($result1)); if ($result2 != "") { $object2 = json_decode($result2); //echo $object2->data[0]->lab_id; // 5 // ... } else { // probably some sort of error. check what's in $object1 }Probably no way to change the output though, huh? See if you can: returning two distinct JSON values is highly irregular, and instead both should be represented in a single object like { "result": { "CODE": "01", "MESSAGE": "Certificate Found" }, "data": [ { "lab_id": "5", ... } ] } Quote Link to comment Share on other sites More sharing options...
gizmola Posted March 30, 2017 Share Posted March 30, 2017 Thanks Gizmola for your response, like I mentioned that I am new to this Web Service and unfortunately I had to do this urgently. From your explaination I started thinking that perhaps the problem is with the JSON Results from the web service I am trying to consume this from, Now, perhaps if I post the the results as they are and the code I am trying to display the fields from these results, perhaps you guys may be able to help me because I struggled with this the whole of yesterday. Here below are the JSON Results followed by the actual code to display fields: I stated that specifically I thought. 2 Json blocks returned rather than one. You can work around it as per Requinix's code, or even more complicated code, but the fact of the matter is, that you shouldn't have to. Is this webservice something internal or related to the project? I would be asking them wtf they are returning 2 blocks of json instead of one! Working around something stupid like this should be a last resort. Quote Link to comment Share on other sites More sharing options...
darklexus2k9 Posted April 7, 2017 Share Posted April 7, 2017 (edited) quick question for every one here why dose it have to be so complicated ??use regex to replace } { found after message and before data with , this then takes servers out put of {"CODE":"01","MESSAGE":"Certificate Found"} {"data":[{"lab_id":"9","lab_no":"PPP018","lab_parent":"BVA018","lab_name":"Molanganiii (Pty) Ltd","comp_reg_no":"","lab_description":"CODES OF GOOD PRACTICE","disciplines":"The facility complies with the requirements","phys_1":"Mile Road Park","phys_2":"Ruwanda","phys_city":"Mali","phys_code":"9999","province":"Mule","updated":"2011-01-25 16:01:45"}]} and give you proper functioning json of {"CODE":"01","MESSAGE":"Certificate Found","data":[{"lab_id":"9","lab_no":"PPP018","lab_parent":"BVA018","lab_name":"Molanganiii (Pty) Ltd","comp_reg_no":"","lab_description":"CODES OF GOOD PRACTICE","disciplines":"The facility complies with the requirements","phys_1":"Mile Road Park","phys_2":"Ruwanda","phys_city":"Mali","phys_code":"9999","province":"Mule","updated":"2011-01-25 16:01:45"}]} you should then be able to access data using json decode pritty easy with no errors ?i mean simply doing something like preg_replace("/}[[:space:]]+{/", ",", $input_lines); should work fine and give you valid json to decode Edited April 7, 2017 by darklexus2k9 Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted April 7, 2017 Share Posted April 7, 2017 From all the crappy workarounds you could have picked, this is probably the worst. You're blindly replacing any pairs of braces within the response, so if the JSON document contains other objects, you're fudging up those as well. Even worse: You blindly assume that the two JSON documents don't have common keys. If they do, you end up with duplicate keys. And then again: I'm more interested in why the server sends an invalid response in the first place. This is a defect which should be fixed, not worked around. Quote Link to comment Share on other sites More sharing options...
darklexus2k9 Posted April 7, 2017 Share Posted April 7, 2017 going by the responce froom the server i am guessing nothing is broken or wrong i am guessing the company that makes the output has there own program that looks for each line on its own and works on that and does not work on it as the one big string wouldnt be the first company i have seen so this in fact itv in the uk use todo something along the same lines with there ios api they had 3 diffrent json responces returned in the one request there app simply treated each line in the responce as a new json object to be decoded and displayedas for the work around yes it is messy and could affect other things but bassed on the returned json and going by what the user has said i assume the respone is generaly the same kind of output for all requests and there for should work fine for what he needes todo for now if things become more complex then the user would need to post the other possible outputs to determin what would be needed but for now be it messy or not it should work and be the most simle way for the users todo what they need todo Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted April 8, 2017 Share Posted April 8, 2017 (edited) You make a lot of assumptions. I care about facts. Multiple JSON documents on multiple lines don't conform to any standard format, so it's worth asking whether that's actually intended and why it's implemented that way. The same data could easily be represented with a single valid JSON document. If the format is in fact designed like that, then the parser should follow the rules of the actual format (e. g. iterate over the lines and decode them individually), not make assumptions. // By the way, proper interpunction would make your texts a lot more readable. Edited April 8, 2017 by Jacques1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.