Using file_get_contents I get headers from sites
function page_title($url) { $fp = file_get_contents($url); if (!$fp) return null; $res = preg_match("/<title>(.*)<\/title>/siU", $fp, $title_matches); if (!$res) return null; // Clean up title: remove EOL and excessive whitespace. $title = preg_replace('/\s+/', ' ', $title_matches[1]); $title = trim($title); return $title; } Often there are sites without headers, I have default headers that I can save to the database so that 'my_title' => $item->my_title,
$db->add(array_merge($item->only(['title', 'wareUrl', 'description', 'user', 'user_id', 'youtube', 'activatedate']), [ 'id' => (4 * 10000000) + $item->id, 'item_id' => $item->id, 'type' => 4, 'user_id' => $item->user['id'], 'youtube' => video_youtube(''.$item->wareUrl.''), 'title' => page_title(''.$item->wareUrl.''), // 'my_title' => $item->my_title, 'activatedate' => $item->activatedate, 'time' => microtime(true) ])); Help to create a construction } else { , so that in the absence of a header received via file_get_contents, the default header 'my_title' => $item->my_title,
Tried to set if inside $db->add(array_merge($item->only(['title', 'wareUrl', 'description', 'user', 'user_id', 'youtube', 'activatedate']), [ getting an error.