Browse Source

Working with response array

Md Mozahidur Rahman 1 year ago
parent
commit
60f5e9ea03
1 changed files with 67 additions and 63 deletions
  1. 67 63
      page-template/page-journal.php

+ 67 - 63
page-template/page-journal.php

@@ -14,21 +14,33 @@ get_header(); ?>
     $client_id = get_field('client_id', 'option');
     $journal_api = get_field('journal_api', 'option');
 
-    $response_employeeJournal = wp_remote_post(
-        $journal_api,
-        array(
-            'body' => array(
-                // 'employee_id' => "SS",
-                "year"  => "",
-                'user_login_id' => $client_id,
-                'auth_token' => $auth_token,
-                'page' => 1,
-                'limit' => 2
-            )
-
-        )
-    );
-    $employeeJournal = json_decode(wp_remote_retrieve_body($response_employeeJournal), true);
+    $curl = curl_init();
+
+    curl_setopt_array($curl, array(
+        CURLOPT_URL => $journal_api,
+        CURLOPT_RETURNTRANSFER => true,
+        CURLOPT_ENCODING => '',
+        CURLOPT_MAXREDIRS => 10,
+        CURLOPT_TIMEOUT => 0,
+        CURLOPT_FOLLOWLOCATION => true,
+        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
+        CURLOPT_CUSTOMREQUEST => 'POST',
+        CURLOPT_POSTFIELDS => '{
+            "employee_id" : " ",
+            "year" : "2022",
+            "auth_token" : "' . $auth_token . '",
+            "user_login_id" : "' . $client_id . '",
+            "page" : 1,
+            "limit" : 3
+        }',
+        CURLOPT_HTTPHEADER => array(
+            'Content-Type: application/json'
+        ),
+    ));
+
+    $response = curl_exec($curl);
+    curl_close($curl);
+    $response = json_decode($response, true);
 
 ?>
 
@@ -114,63 +126,55 @@ get_header(); ?>
                             </div>
 
                             <?php
-
-                            // echo $auth_token;
-                            // var_dump($employeeJournal);
-
-                            // echo '<pre>';
-                            // print_r($employeeJournal);
-
+                            foreach ($response as $item) {
+                                // Access individual elements of each item in the array
+                                $year = $item['Year'];
+                                $journalList = $item['JournalList'];
+
+                                // Process or output the values as needed
+                                echo "Year: $year\n";
+                                echo '<br>';
+
+                                // Iterate over the JournalList array within each item
+                                foreach ($journalList as $journal) {
+                                    // Access individual elements of each journal
+                                    $paperTitle = $journal['PaperTitle'];
+                                    $JournalLink = $journal['JournalLink'];
+                                    $Keyword = $journal['Keyword'];
+                                    $journalTitle = $journal['JournalTitle'];
+
+                                    // Process or output the values as needed
+                                    echo "Paper Title: $paperTitle\n";
+                                    echo "Journal Link: $JournalLink\n";
+                                    echo "Keywords: $Keyword\n";
+                                    echo "Journal Title: $journalTitle\n";
+                                    echo '<br>';
+
+                                    // Iterate over the Author array within each journal
+                                    foreach ($journal['Author'] as $author) {
+                                        // Access individual elements of each author
+                                        $authorName = $author['AuthorName'];
+                                        // Process or output the values as needed
+                                        echo "Author Name: $authorName\n";
+                                        echo '<br>';
+                                    }
+                                }
+                            }
                             ?>
 
                             <?php
-
-                            $curl = curl_init();
-
-                            curl_setopt_array($curl, array(
-                                CURLOPT_URL => 'http://103.109.52.3/webapi/api/EmployeeJournal',
-                                CURLOPT_RETURNTRANSFER => true,
-                                CURLOPT_ENCODING => '',
-                                CURLOPT_MAXREDIRS => 10,
-                                CURLOPT_TIMEOUT => 0,
-                                CURLOPT_FOLLOWLOCATION => true,
-                                CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
-                                CURLOPT_CUSTOMREQUEST => 'POST',
-                                CURLOPT_POSTFIELDS => '{
-                                    "employee_id" : " ",
-                                    "year" : "2022",
-                                    "user_login_id" : "api_user",
-                                    "auth_token" : "api_user20230419045143",
-                                    "page" : 1,
-                                    "limit" : 3
-                                }',
-                                CURLOPT_HTTPHEADER => array(
-                                    'Content-Type: application/json'
-                                ),
-                            ));
-
-                            $response = curl_exec($curl);
-                            curl_close($curl);
-
-                            $elm = json_decode($response, true);
                             echo '<pre>';
-                            print_r($elm[0]["Year"]);
+                            print_r($response[0]["Year"]);
                             echo '<br>';
-                            print_r($elm[0]["JournalList"][0]["PaperTitle"]);
+                            print_r($response[0]["JournalList"][0]["PaperTitle"]);
                             echo '<br>';
-                            print_r($elm[0]["JournalList"][0]["JournalLink"]);
+                            print_r($response[0]["JournalList"][0]["JournalLink"]);
                             echo '<br>';
-                            print_r($elm[0]["JournalList"][0]["Keyword"]);
+                            print_r($response[0]["JournalList"][0]["Keyword"]);
                             echo '<br>';
-                            print_r($elm[0]["JournalList"][0]["JournalTitle"]);
+                            print_r($response[0]["JournalList"][0]["JournalTitle"]);
                             echo '<br>';
-                            print_r($elm[0]["JournalList"][0]["Author"][0]["AuthorName"]);
-                            // echo '<br>';
-                            // print_r($elm[0]["JournalList"][0]["Author"]);
-                            // echo '<br>';
-                            // print_r($elm[0]["JournalList"][0]);
-                            // echo '<br>';
-                            // print_r($elm[0]);
+                            print_r($response[0]["JournalList"][0]["Author"][0]["AuthorName"]);
                             ?>
 
                         </div>