I have the following code which basically runs a bunch of curl request in a for loop
This is a background system job.. however when this code is running.. i can't access the website, website is just loading until this task/function is finished
public function endcount() {
$order_list = $this->order_model->get_rows([
'where' => [
"status IN ('Processing', 'In Progress') AND service_id = 1"
],
'order_by' => 'quantity ASC',
'limit' => '200'
]);
foreach ($order_list as $key => $value) {
$start_count = $value['start_count'];
//service id = 1 is for followers
if ($value['service_id'] == '1' || $value['service_id'] == '3' || $value['service_id'] == '5') {
$end_count = 0;
try {
$username = $value['target'];
$proxy = '139.99.54.49:10163';
$proxyauth = 'username:password';
$url = 'https://www.instagram.com/'. $username .'/?__a=1&__d=dis';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_PROXY, $proxy); // PROXY details with port
curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxyauth); // Use if proxy have username and password
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json') );
$data = curl_exec($ch);
if (curl_errno($ch)) {
$error_msg = curl_error($ch);
var_dump($error_msg);
}
$json_data = json_decode($data, true);
if ($json_data && array_key_exists('graphql', $json_data)) {
$end_count = $json_data['graphql']['user']['edge_followed_by']['count'];
} else {
var_dump($username . ' data ga bisa');
$end_count = 0;
}
curl_close($ch);
} catch(Exception $e) {
var_dump($e->getMessage());
$end_count = 0;
}
if ($end_count == -666) {
$end_count = $start_count + 5;
}
$total_followers = $start_count + $value['quantity'];
$remains = $total_followers - $end_count;
if ($remains <= 10) {
$status = 'Success';
} else {
$status = 'Partial';
}
var_dump('end count of ' . $value['target'] . ' ' . $end_count . ' WITH REMAINS: ' . $remains);
if ($status == 'Success') {
$update_order = [
'remains' => $remains,
'status' => $status,
'updated_at' => date('Y-m-d H:i:s'),
];
$update_order = $this->order_model->update($update_order, ['id' => $value['id']]);
if ($update_order == true) {
print('ID '.$value['id'].' => ['.$status.'] - [FINAL COUNT : '.$end_count.'] - [REMAINS : '.$remains.'] - [TARGET COUNT : '.$total_followers.']<br>');
} else {
print('Error..');
}
}
//var_dump($value['target']);
} else if ($value['service_id'] == '2') {
//service id = 2 is for likers
}
}
}
is there any way i can optimize this code so it is not blocking access to the site while running ?
try adding sleep(5); funtion to your code so server isn't overloaded with requests..
your code should look something like this :
foreach ($order_list as $key => $value) {
sleep(5);
$start_count = $value['start_count'];
Related
API code executes successfully without giving any errors however the voucher am trying to create doesn't get created in datto below is the response am getting ,the highlighted text is the voucher am trying to create what might be the cause of this because the input body am sending is as indicate in their documentation https://github.com/cloudtrax/docs/blob/master/api/vouchers.md#actions
this is the response and the request code any help will be highly appreciated
<?php
$key = 'xxxxxxxx';
$secret='xxxxxxx';
class Method
{
const GET = 0;
const POST = 1;
const PUT = 2;
const DELETE = 3;
public static function nameForEnumValue($value) {
switch($value) {
case 0: return "GET";
case 1: return "POST";
case 2: return "PUT";
case 3: return "DELETE";
}
}
};
function print_debug_info($method, $endpoint, $headers) {
print( "\n" );
print( "Method: " . Method::nameForEnumValue($method) . "\n");
print( "Endpoint: " . $endpoint . "\n" );
print_r($headers);
}
function build_headers($auth, $sign) {
$headers = array();
$headers[] = "Authorization: " . $auth;
$headers[] = "Signature: " . $sign;
$headers[] = "Content-Type: application/json";
$headers[] = "OpenMesh-API-Version: 1";
return $headers;
}
function invoke_curl($method, $endpoint, $headers, $json) {
$api_server = 'https://api.cloudtrax.com';
try {
// get a curl handle then go to town on it
$ch = curl_init($api_server . $endpoint);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
if ($method == Method::DELETE)
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
elseif ($method == Method::PUT) {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
}
else if ($method == Method::POST) {
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
}
$result = curl_exec($ch);
if ($result == FALSE) {
if (curl_errno($ch) == 0)
echo "#### NOTE ####: nil HTTP return: This API call appears to be broken" . "\n";
else
throw new Exception(curl_error($ch), curl_errno($ch));
}
else
echo "RESULT: \n" . $result . "\n";
echo "RESULT: \n" . $json . "\n";
}
catch(Exception $e) {
trigger_error( sprintf('Curl failed with error #%d: "%s"',
$e->getCode(), $e->getMessage()), E_USER_ERROR);
}
}
function call_api_server($method, $endpoint, $data) {
global $key, $secret;
$time = time();
$nonce = rand();
if ($method == Method::POST)
assert( '$data != NULL /* #### POST requires $data #### */');
elseif ($method == Method::GET || $method == Method::DELETE)
assert( '$data == NULL /* ### GET and DELETE take no $data ### */');
$path = $endpoint;
// if present, concatenate encoded json to $endpoint for Signature:
if ($data != NULL) {
$json = json_encode($data);
$path .= $json;
}
$authorization = "key=" . $key . ",timestamp=" . $time . ",nonce=" . $nonce;
$signature = hash_hmac('sha256', $authorization . $path . $body, $secret);
$headers = build_headers($authorization, $signature);
print_debug_info($method, $endpoint, $headers);
invoke_curl($method, $endpoint, $headers, $json);
}
$uniqueKey=strtoupper(substr(sha1(microtime()), rand(0, 6), 6));
$uniqueKey = implode("-", str_split($uniqueKey, 6));
echo($uniqueKey);
$data =array('desired_vouchers',[
'code'=>$uniqueKey,
//'code'=>0103,
'duration'=> 24,
'max_users'=> 4,
'up_limit'=> 20,
'down_limit'=> 40,
'comment'=> "Courtesy of API Test",
'purge_days'=> 5
] );
call_api_server(Method::POST, "/voucher/network/xxxxx",$data);
//call_api_server(Method::GET, "/dna-api/v1/login",Null);
//call_api_server(Method::GET, "/dna-api/doc#get--dna-api-v1-routers-{mac}",Null);
//call_api_server(Method::GET, "/dna-api/v1/users/xxxx/overview",Null);
?>
I have a project to make shopee product scraping. Scraping for some products is successful, but if there are thousands of products, only hundreds of products are successful, the rest fail and the error is "forbidden". I've tried using three php methods for scraping, namely curl_init, curl_multi_init, and curl class.
php curl_init() This method returns an array
function scrapcurl($data){
$result = [];
foreach ($data as $key => $value) {
$url = $value;
$ua = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.A.B.C Safari/525.13';
$handle = curl_init();
// Set the url
curl_setopt($handle, CURLOPT_URL, $url);
curl_setopt($handle, CURLOPT_USERAGENT, $ua);
curl_setopt($handle, CURLOPT_HEADER, 0);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($handle);
curl_close($handle);
array_push($result, $output);
}
return $result;
}
php curl_multi_init() This method returns an array of json in string
ex: {"error":null,"error_msg":null,"data":{"itemid":14513803134,"shopid":40261202,"userid":0,...} then i convert to array associative with another function
function multiRequest($data, $options = array()) {
// array of curl handles
$curly = array();
// data to be returned
$result = array();
// multi handle
$mh = curl_multi_init();
// loop through $data and create curl handles
// then add them to the multi-handle
foreach ($data as $id => $d) {
$curly[$id] = curl_init();
$url = (is_array($d) && !empty($d['url'])) ? $d['url'] : $d;
curl_setopt($curly[$id], CURLOPT_URL, $url);
curl_setopt($curly[$id], CURLOPT_HEADER, 0);
curl_setopt($curly[$id], CURLOPT_RETURNTRANSFER, 1);
// post?
if (is_array($d)) {
if (!empty($d['post'])) {
curl_setopt($curly[$id], CURLOPT_POST, 1);
curl_setopt($curly[$id], CURLOPT_POSTFIELDS, $d['post']);
}
}
// extra options?
if (!empty($options)) {
curl_setopt_array($curly[$id], $options);
}
curl_multi_add_handle($mh, $curly[$id]);
}
// execute the handles
$running = null;
do {
curl_multi_exec($mh, $running);
} while($running > 0);
// get content and remove handles
foreach($curly as $id => $c) {
$result[$id] = curl_multi_getcontent($c);
curl_multi_remove_handle($mh, $c);
}
// all done
curl_multi_close($mh);
return $result;
}
Curl class This method returns an array
use Curl;
function scrap($data)
{
$resultawal=[];
$result=[];
$image=[];
foreach ($data as $key => $value) {
# code...
$curl = new Curl();
$curl->get($value);
if ($curl->error) {
# code...
echo 'Error: ' . $curl->errorCode . ': ' . $curl->errorMessage . "\n";
}
else {
# code...
$js = $curl->response;
foreach ($js->data->images as $key => $value) {
$image["img$key"] = $value;
};
$gambar1 = json_encode($image);
$harga = substr($js->data->price_max, 0, -5);
$stok = $js->data->stock;
$nama = str_replace("'", "", $js->data->name);
$catid = $js->data->catid;
$deskripsi = str_replace("'", "", $js->data->description);
if ($js->data->video_info_list != '') {
$video = $js->data->video_info_list;
$video1 = json_encode($video);
} else {
$video1 = null;
}
$linkss = "https://shopee.co.id/" . str_replace(" ", "-", $nama) . "-i." . $js->data->shopid . "." . $js->data->itemid;
$berat = 0; // berat
$min = 1; // minimum_pemesanan
$etalase = NULL; // etalase
$preorder = 1; //preorder
$kondisi = "Baru";
$sku = NULL;
$status = "Aktif";
$asuransi = "optional";
$item_id = $js->data->itemid;
$resultawal = array(
'item_id'=>$item_id,
'linkss'=>$linkss,
'nama'=>$nama,
'deskripsi'=>$deskripsi,
'catid'=>$catid,
'berat'=>$berat,
'min'=>$min,
'etalase'=>$etalase,
'preorder'=>$preorder,
'kondisi'=>$kondisi,
'gambar1'=>$gambar1,
'video1'=>$video1,
'sku'=>$sku,
'status'=>$status,
'stok'=>$stok,
'harga'=>$harga,
'asuransi'=>$asuransi,
);
array_push($result, $resultawal);
}
}
return $result;
}
My Question
From the three methods above, when the link is thousands, why does a 403 forbidden error appear with methods 1 and 2, and error: 403: HTTP/2 403 with method 3??
Additional info:
Input of the program is thousand of link of products. For example:
5Pcs-pt4115-4115-sot-89-IC-Power-IC-LED-i.41253123.1355347598.sp_atk=09264df0-bb8d-4ca5-8970-719bbb2149dd
and then i take the shopid=41253123 and itemid=1355347598. Then i put to this link:
$link = "https://shopee.co.id/api/v4/item/get?itemid=" . $item_id . "&shopid=" . $shop_id;
and then use three methods above to scrape the product data.
i want to import excel as json data in Codeigniter with validation for each row.
excel column
|A |B |C
|demo|DM1|11231
|demo|DM2|87128
...
my code for get array from excel
//start loop excel from 2nd row. Row 1 is title row
for ($j=2; $j < $lastRow; $j++ ){
$myArray[] = array(
'site_id' => $objWorksheet->getCell('B'.$j)->getValue(),
'site_name' => $objWorksheet->getCell('A'.$j)->getValue(),
'id_site_doc'=> $objWorksheet->getCell('C'.$j)->getValue()
}
//validate the array
$this->form_validation->set_data($myArray);
$this->form_validation->reset_validation();
foreach ($myArray as $key => $value) {
$columnB = $myArray[$key]['site_id'];
$columnA = $myArray[$key]['site_name'];
$columnC = $myArray[$key]['id_site_doc'];
}
if (empty($columnB )){
$this->form_validation->set_rules('mr_submit_target', 'site_id on row ' . $j, 'required');
}
else if (empty($columnA )){
$this->form_validation->set_rules('short_desc', 'site_name on row ' . $j, 'required');
}
else if (empty($columnC )) {
$this->form_validation->set_rules('cd_id', 'id_site_doc on row ' . $j, 'required');
}
if ($this->form_validation->run() == FALSE){
$errorArray[$j]=$this->form_validation->error_array();
print_r($errorArray[$j]);
}else{
//post to endpoint
$data_to_post = json_encode($myArray);
$curl = curl_init('http://myendpoint/implementation_bom_op');
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_to_post))
);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data_to_post);
// Send the request
$result = curl_exec($curl);
echo $result;
}
}
my expectation
the data will be post only if have full validation, when there are blank cell in one of each cell it will throw error. is that possible? or it should be on endpoint side?
//start loop excel from 2nd row. Row 1 is title row
for ($j=2; $j < $lastRow; $j++ ){
$columnB = $objWorksheet->getCell('B'.$j)->getValue();
$columnA = $objWorksheet->getCell('A'.$j)->getValue();
$columnC = $objWorksheet->getCell('C'.$j)->getValue();
//validate the array
$error = 0;
if($columnB == ""){
$errorArray[$j][] = 'your-error-here';
$error +=1;
}
if($columnA == ""){
$errorArray[$j][] = 'your-error-here';
$error +=1;
}
if($columnC == ""){
$errorArray[$j][] = 'your-error-here';
$error +=1;
}
if($error != 0){
continue; //do something with the error
}else{
$myArray['site_id'] = $columnB;
$myArray['site_name'] = $columnA;
$myArray['id_site_doc'] = $columnC;
}
}
//post to endpoint
$data_to_post = json_encode($myArray);
$curl = curl_init('http://myendpoint/implementation_bom_op');
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_to_post))
);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data_to_post);
// Send the request
$result = curl_exec($curl);
echo $result;
This should work for you.
I wrote a script that communicates with a popular game, called kahoot. It logs in and answer's each question, and then echo's when it is finished. Here is it working on the command line. However, when I add this functionality to my website, when I run it it gives me a 504 gateway timeout, even though before the timeout it should have produced some output and echoed it to the page.
Here is the website code:
Full execution code:
<html>
<head>
<title>Kahoot bot</title>
</head>
<body>
<?php
$username = 'kahootbot28#gmail.com';
$password = 'botkahoot28';
$loginUrl = 'https://create.kahoot.it/rest/authenticate';
$kahootId = $_GET['quizid'];
$type = $_GET['what'];
if ($type == "bot") {
$call = "~/www/reteps.tk/go/kahoot-auto " . $_GET['gamepin'] . " " . $_GET['username'] . " ";
echo($call);
}
$pageUrl = 'https://create.kahoot.it/rest/kahoots/' . $kahootId;
$loginheader = array();
$loginheader[] = 'content-type: application/json';
$loginpost = new stdClass();
$loginpost->username = $username;
$loginpost->password = $password;
$loginpost->grant_type = "password";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $loginUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($loginpost));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER,$loginheader);
$store = curl_exec($ch);
curl_close($ch);
$token = json_decode($store,true)["access_token"];
//get questions
$quizheader = array();
$quizheader[] = 'authorization: ' . $token;
$options = array(
'http' => array(
'method' => 'GET',
'header' => "Authorization: ".$token."\r\n"
)
);
$pairs = array(
0 => "red",
1 => "blue",
2 => "yellow",
3 => "green",
);
$context = stream_context_create($options);
$raw_result = file_get_contents($pageUrl, false, $context);
$result = json_decode($raw_result,true)["questions"];
echo("<a href='kahoot_bot'>back</a><br>");
foreach($result as $value) {
if ($type == "text") {
echo($value['question']." ");
}
$choices = $value['choices'];
for($i=0;$i<count($choices);$i++) {
if ($choices[$i]['correct'] == true) {
if ($type == "text") {
echo($choices[$i]['answer']."<br>");
} elseif ($type == "bot") {
$call .= $i;
} else {
echo($pairs[$i].", ");
}
break 1;
}
}
}
if ($type == "bot") {
$old_result = "";
$handle = popen($call . " 2>&1", "r");
$result = fread($handle, 2096);
echo($result);
while ((strpos($result, "end") !== false) != true) {
$result = fread($handle, 2096);
sleep(1);
if ($result != $old_result) {
echo($result);
$old_result = $result;
}
}
pclose($handle);
}
?>
</body>
</html>
I am trying to crawl images from a website using PHP.
The page that I am trying to crawl is:
http://www.reebonz.com.sg/event/t7349#/event/t7349
But using my code I only get the href of my header. My code is:
<?php
require_once ('function.php');
$advt_id = "88477";
$programurl = "http://www.reebonz.com.sg/event_list/1/";
$baseurl = "http://www.reebonz.com.sg/event_list/1/";
$crawl_data []= array ( "department" => 0, "category" => "bags" , "advt_cat" => "BALENCIAGA", "cat_url" => 'http://www.reebonz.com.sg/event/t7349#/event/t7349');
$data = get_data($url);
$product_raw = splice_data ($data, 'ul class="rec-items-ul ng-scope"',1, '</ul>',1);
$product_list = splice_list ($product_raw, 'href="', '"');
echo "\n**** Got Product List ".count($product_list)." ***\n";
print_r ($product_list);
foreach ($product_list as $product)
{
if ((strlen($product) < 10))
{
echo $product;
continue;
}
$url = $baseurl.$product;
$data = get_data($url);
$img_data = splice_data ($data, 'class="rbz_product-zoom-image row"', 1, '</div>', 1);
$img_url = splice_data ($img_data, 'href="',1, '"', 1);
echo $img_url;
$filePath = $crawl_cat['category']."\\".$crawl_cat['advt_cat'];
if (!file_exists($filePath)) {
mkdir($filePath, 0777, true);
}
grab_image($img_url,$filePath);
//grab_image($img_url5,$filePath);
echo "*";
}// end of product insert for
?>
the function.php is:
function splice_data ($data, $startstr, $startoccur, $endstr, $endoccur)
{
if ($startoccur > 1)
{
for ($i = 1, $startpos = 1 ; $startoccur >= $i; $i++, $startpos++)
{
$startpos = stripos($data,$startstr,$startpos);
//echo $startpos. "\n";
}
$start = $startpos;
}
else
$start = stripos($data,$startstr,$startoccur);
$start_index = strlen($startstr);
$end = stripos($data,$endstr,$start + $start_index ) ;
$splice_data = substr($data,$start + $start_index, $end - ($start + $start_index) );
return $splice_data;
}
function splice_list ($img_data, $start_str, $end_str, $find = '', $replace = '')
{
for ($i = 1, $j = 1; stripos($img_data,$start_str,$i) > 1 ;)
{
$start = stripos($img_data,$start_str,$i);
$start_len = strlen($start_str);
$end = stripos($img_data,$end_str,$start + $start_len) ;
$data_list[] = str_replace($find,$replace,substr($img_data, $start + $start_len , $end - $start - $start_len)) ;
$i = $end;
$j++;
}
$result = array_unique($data_list);
return $result;
}
function get_data($url, $ckfile="", $cookie="")
{
$toCheckURL = $url;
// This all sets up the CURL actions to check the page
$header=array(
// 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12',
'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language: en-us,en;q=0.5',
'Accept-Encoding: gzip,deflate',
'Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7',
'Keep-Alive: 115',
'Connection: keep-alive',
);
$proxies = array();
$ch = curl_init();
if (isset($proxy)) { // If the $proxy variable is set, then
curl_setopt($ch, CURLOPT_PROXY, $proxy); // Set CURLOPT_PROXY with proxy in $proxy variable
}
curl_setopt($ch, CURLOPT_URL, $toCheckURL);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, false);
if (isset($ckfile) && $ckfile !="" and !empty($ckfile))
{
curl_setopt ($ch, CURLOPT_COOKIEFILE, $ckfile);
}
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT,60);
curl_setopt($ch, CURLOPT_TIMEOUT,90);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10); //follow up to 10 redirections - avoids loops
if($cookie != "")
curl_setopt($ch,CURLOPT_HTTPHEADER, array($cookie));
curl_setopt($ch,CURLOPT_USERAGENT,$agents[array_rand($agents)]);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
The output that I get now is:
**** Got Product List 8 *** Array ( [0] => //netdna.bootstrapcdn.com/twitter-
bootstrap/2.3.2/css/bootstrap-combined.no-icons.min.css [1] => //netdna.bootstrapcdn.com/font-
awesome/3.2.1/css/font-awesome.css [2] =>
http://www.reebonz.com.sg/sites/all/themes/custom/octopus2/xfavicon.ico.pagespeed.ic.jT8Y7LgYBc.png
[3] => http://www.octopus2.local/sites/all/themes/custom/octopus2/css/reebonz-ie.css [4] =>
http://www.reebonz.com.sg/sites/all/modules,_contrib,_jquery_update,_replace,_ui,_themes,_base,_minified,_jquery.ui.core.min.css,qn1a78z+modules,_contrib,_jquery_update,_replace,_ui,_themes,_base,_minified,_jquery.ui.theme.min.css,qn1a78z+modules,_contrib,_jquery_update,_replace,_ui,_themes,_base,_minified,_jquery.ui.slider.min.css,qn1a78z+modules,_contrib,_panels,_css,_panels.css,qn1a78z+modules,_custom,_mailcheck,_css,_mailcheck.css,qn1a78z+themes,_custom,_octopus2,_css,_bootstrap.css,qn1a78z+themes,_custom,_octopus2,_css,_reebonz-core.css,qn1a78z+themes,_custom,_octopus2,_css,_reebonz-social-network.css,qn1a78z+themes,_custom,_octopus2,_css,_reebonz-....
What is wrong with my code?Is there any simple way to do this??
use php DomDocument:
$doc = new DOMDocument();
$doc->loadHTML(your_html_code);
$images = $doc->getElementsByTagName('img');
foreach ($images as $img) {
//do whatever you like
}
Download this library : http://sourceforge.net/projects/simplehtmldom/
And the below code will work
(include that library in top)
<?php
error_reporting(1);
include_once('simple_html_dom.php');
$html = new simple_html_dom();
$html->load_file('https://www.google.co.in/search?q=shahrukh+khan&newwindow=1&biw=1375&bih=791&source=lnms&tbm=isch&sa=X&sqi=2&ved=0ahUKEwi1rO6AjZrKAhWSBY4KHWSGBDQQ_AUIBygC');
$reviews = $html->find('img');
$fetched_images = '';
foreach($reviews as $link)
{
//find review ID if not null
if($link->{'src'} != ''){
$review_ID = $link->{'src'};
$fetched_images[] = $review_ID;
}
}
?>
<ul>
<?php foreach ($fetched_images as $fetched_image): ?>
<li style="display:inline-block"><img src="<?php echo $fetched_image;?>"></li>
<?php endforeach ?>
</ul>
<?php
include_once('simple_html_dom.php');
$target_url = "Your URL here";
$html = new simple_html_dom();
$html->load_file($target_url);
$images = $html->find('img');
/**foreach($images as $link){
//find review ID if not null
if($link->{'src'} != ''){
$image_ID = $link->{'src'};
$fetched_images[] = $image_ID;
}
}*/
foreach ($images as $fetched_image){
echo $fetched_image;
}
?>