$url = "http://localhost/dss/dss.asmx/AreaCategory_list";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36');
$xmlstr = curl_exec($ch);
curl_close($ch);
var_dump( $xmlstr);
cannot show data
$json2 = "http://localhost/dss/dss.asmx/AreaCategory_list";
$array = json_decode($json2);
echo $array->Remark;
Related
I have a code:
$client = new \GuzzleHttp\Client();
$response = $client->request('GET', 'http://example.com');
$meta $response->body(true)['meta']; //get meta description
I need get meta description content from a site. How I can do it? My solution is not working.. I get error: undefined index meta
You don't need Guzzle
$tags = get_meta_tags('http://www.example.com/');
echo $tags['author'];
echo $tags['keywords'];
echo $tags['description'];
or
$content = $response->body();
$tags = get_meta_tags($content);
Using get_meta_tags function
ini_set('user_agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:7.0.1) Gecko/20100101 Firefox/7.0.1');
$meta_tags = get_meta_tags('www.example.com');
Using curl
$user_agent = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36';
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_USERAGENT, $user_agent);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, "https://example.com");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$data = curl_exec($ch);
curl_close($ch);
print_r($data);
I have this data sheet from angular that I want to convert to sting to put in a table sql database...
URL= 'http://lscluster.hockeytech.com/feed/index.php?feed=statviewfeed&view=players&season=60&team=all&position=skaters&rookies=0&statsType=expanded&rosterstatus=undefined&site_id=1&first=0&limit=1185&sort=points&league_id=4&lang=en&division=-1&key=50c2cd9b5e18e390&client_code=ahl&league_id=4&callback=angular.callbacks._q'
Normaly with that code I'm able to parse everything but it returns zero for that URL...
$curl_handle=curl_init();
curl_setopt($curl_handle, CURLOPT_URL, $URL);
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handle, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.80 Safari/537.36');
$querymain = curl_exec($curl_handle);
curl_close($curl_handle);
$arr = json_decode((string)$querymain, true);
(json_last_error()); // call after json_decode
$numarr = count($arr['data']);
thanks for the tips.
I am trying to export the below url meta tags but it is not working it giving below result
Warning: get_meta_tags(https://www.washingtonpost.com/politics/white-house-reels-as-fbi-director-contradicts-official-claims-about-alleged-abuser/2018/02/13/f010f256-10d9-11e8-9570-29c9830535e5_story.html?tid=pm_pop): failed to open stream: Redirection limit reached, aborting in.
Any ideas for this ?
For start you need to make a call to the 1st page to set the cookie else it's not going to work
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,FALSE);
curl_setopt($ch,CURLOPT_URL,"https://www.washingtonpost.com");
curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE);
curl_setopt($ch, CURLOPT_USERAGENT, "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");
$cookieName = "";
if(isset($_COOKIE['PHPSESSID'])){
$cookieName = $_COOKIE['PHPSESSID'];
}
curl_setopt( $ch, CURLOPT_COOKIEJAR, $_SERVER['DOCUMENT_ROOT'].'/logs/'.$cookieName.'.txt');
curl_setopt( $ch, CURLOPT_COOKIEFILE, $_SERVER['DOCUMENT_ROOT'].'/logs/'.$cookieName.'.txt');
curl_exec($ch);
curl_close($ch);
then a second call to get the actual page
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,FALSE);
curl_setopt($ch,CURLOPT_URL,"https://www.washingtonpost.com/politics/white-house-reels-as-fbi-director-contradicts-official-claims-about-alleged-abuser/2018/02/13/f010f256-10d9-11e8-9570-29c9830535e5_story.html?tid=pm_pop");
curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE);
curl_setopt($ch, CURLOPT_USERAGENT, "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");
$cookieName = "";
if(isset($_COOKIE['PHPSESSID'])){
$cookieName = $_COOKIE['PHPSESSID'];
}
curl_setopt( $ch, CURLOPT_COOKIEJAR, LOG_DIR.'/'.$cookieName.'.txt');
curl_setopt( $ch, CURLOPT_COOKIEFILE, LOG_DIR.'/'.$cookieName.'.txt');
$page = curl_exec($ch);
curl_close($ch);
and finaly with DOMDocument we parse the dom tree
libxml_use_internal_errors(true);
$siteData = new DOMDocument();
$siteData->loadHTML($page);
$metaElements = $siteData->getElementsByTagName("meta");
if($metaElements->item(0)==null){
echo "ERROR";
}
$meta = array();
for($i=0;$i<$metaElements->length;$i++){
$meta[$i] = array();
for($j=0;$j<$metaElements->item($i)->attributes->length;$j++){
$meta[$i][$j] = array($metaElements->item($i)->attributes->item($j)->name,$metaElements->item($i)->attributes->item($j)->value);
}
}
print_r($meta);
meta are stored in the $meta array
you can beautify this code by organizing curl to function.
Anyone can explain to me what is wrong with the code and how do i get the height value? I am trying to get the height of celebrities. Any suggestions?
Thanks.
My code (Updated with CURL user agent setting as advised):
$url='https://www.google.com/webhp?ie=UTF-8#q=ailee+height';
//Set CURL user agent
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec($ch);
curl_close($ch);
//simple html dom
require_once('lib/simple_html_dom.php');
$html = str_get_html($data);
$height= $html->find('div[class="_eF"]',0)->innertext;
echo $height;
I get empty from the above code. In this case, I want to return:
5' 5" (1.65 m)
The problem is that curl doesn't process JavaScript and Google will show a different webpage when JavaScript is disabled, in this case, the div changes to a span with a different id
<span class="_m3b">1.65 m</span>
Also, the link you were using wasn't working for me.
Try this instead:
<?php
header('Content-Type: text/html; charset=utf-8');
$url='https://www.google.pt/search?q=ailee+height&num=10&gbv=1';
//Set CURL user agent
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec($ch);
curl_close($ch);
require_once('simple_html_dom.php');
$html = str_get_html($data);
$height= $html->find('span[class="_m3b"]',0)->innertext;
echo $height;
//1.65 m
Curl doesn't send cookie from dev server, but when i run script from another server, it's works well. I can't understand what's wrong with dev server.
$curl=curl_init($request);
//$cook = './cook/1.txt';
//curl_setopt($curl, CURLOPT_COOKIEJAR, $cook);
//curl_setopt($curl, CURLOPT_COOKIEFILE, $cook)
curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36");
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER,true);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);
//curl_setopt($curl, CURLOPT_ENCODING, 'UTF-8');
curl_setopt($curl, CURLOPT_VERBOSE,1);
curl_setopt($curl, CURLINFO_HEADER_OUT,1);
curl_setopt($curl, CURLOPT_COOKIE, "departureCity=2; path=/;");
$out = curl_exec($curl);
$info = curl_getinfo($curl);
curl_close($curl);
$info:
Array
(
[request_header] => GET /hotels/greece/showhotel/12_islands_villas_26 HTTP/1.1
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36
Host: SITE
Accept: */*
Referer: SITE
)
there is now cookie data in header