I want to extract data from the api I wrote with file_get_contents and save it to the database. But every time I get different results. When I enter the json page from the browser, the data comes complete every time. But when I do it this way, sometimes 5 sometimes 20 data is brought and recorded in the database. This is completely random. I think I'm going crazy.
data pulling function
function fetchData($id){
$url = "https://mywebsite.com/getcomments/".$id."/100";
$myJsonData = file_get_contents($url);
$json = json_decode($myJsonData);
return $json;
}
database registration section
$json_data = fetchData($td);
$yorumlar = $json_data->result;
foreach($yorumlar as $key => $data)
{
$satir = $data->review;
$name = $data->name;
$data = array(
'comment_post_ID' => $post_id,
'comment_author' => $result,
'comment_author_email' => 'tester@testerz.com',
'comment_content' => $satir,
'user_id' => 3,
'comment_meta' => array('ratingValue', $data->rating),
'comment_approved' => 1,
'comment_type' => 'custom-comment-class'
);
wp_insert_comment($data);
}
Is file_get_contents not working properly? Or I am not sure if I can't use the imported data properly. But isn't it weird that sometimes it returns 5 sometimes 20 sometimes 14 data?