get particular attr from a html string in php - php

i have a punch of links in a string like following
<link type="image/png" href="http://infinitewp.com/wp-content/themes/mystile-child/images/favicon.png" />
<link rel="shortcut icon" type="image/png" href="http://infinitewp.com/wp-content/themes/mystile-child/images/favicon.png" />
<link type="image/png" href="http://infinitewp.com/wp-content/themes/mystile-child/images/favicon.png" />
i need to get href for which link has the rel="shortcut icon"
i can find shortcut icon link using
strpos($content,"shortcut icon")
this code but i need to get the href of this link? how to do this in easy way?

Try this :
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "Your URL");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$html = curl_exec ($ch);
curl_close($ch);
$xml = new DOMDocument();
$xml->strictErrorChecking = FALSE;
$xml->loadHTML($html);
$xpath = new DOMXPath($xml);
$arr = $xpath->query('//link[#rel="shortcut icon"]');
echo $arr[0]['href']; // This is the content of your href.

Related

secure use of authorization token in cURL in PHP

I am developing single page application in PHP. I am fetching products data from external API using cURL. API is secured and I have to provide the token.
I want to know that is it secure way to add this token in my index.php page (like I did in the below example), I don't want that someone else misuse the token by inspecting or getting it from the server
<?php
$ch = curl_init();
$url = "http://example.com/v1/products";
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$headers = array(
'Content-Type:application/json',
'Authorization: Bearer abcxyzabac*******'
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
if($e = curl_error($ch)) {
echo $e;
}
else {
$decoded = json_decode($response, true);
}
curl_close($ch);
?>
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
</head>
<body>
<?php
foreach($decoded as $dec)
{
echo $dec->name;
}
?>
</body>
</html>

How to search XML in PHP got by restful URL

I have got XML response from curl and trying to search some tag value but I am failing. Kindly somebody help me
Please bare as I am new to the PHP
<?php
$DynamicValue = 'KWI';
$url='https://www.example.com/_api/web/lists/getbytitle(\'Stations\')/items?$select=OfficeId&$filter='.urlencode("CityCode eq '" . $DynamicValue . "'");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type: application/xml"));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
$XML = new SimpleXMLElement($result);
foreach($XML->entry as $value)
{
echo $value->OfficeId
}
Output:
<?xml version="1.0" encoding="utf-8"?><feed xml:base="https://www.example.com/_api/" xmlns="http://www.w3.org/2005/Atom" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns:georss="http://www.georss.org/georss" xmlns:gml="http://www.opengis.net/gml"><id>a9f073a2-387e-4ac5-81b9-16b3ae81dba3</id><title /><updated>2017-03-20T08:15:47Z</updated><entry m:etag=""61""><id>Web/Lists(guid'5344b09b-d3f7-44ec-bd52-a8f5a99f23f9')/Items(23)</id><category term="SP.Data.StationsListItem" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" /><link rel="edit" href="Web/Lists(guid'5344b09b-d3f7-44ec-bd52-a8f5a99f23f9')/Items(23)" /><title /><updated>2017-03-20T08:15:47Z</updated><author><name /></author><content type="application/xml"><m:properties><d:OfficeId>KWIKU08AA</d:OfficeId></m:properties></content></entry></feed>
Hope this will work.
$string='<?xml version="1.0" encoding="utf-8"?><feed xml:base="https://www.example.com/_api/" xmlns="http://www.w3.org/2005/Atom" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns:georss="http://www.georss.org/georss" xmlns:gml="http://www.opengis.net/gml"><id>a9f073a2-387e-4ac5-81b9-16b3ae81dba3</id><title /><updated>2017-03-20T08:15:47Z</updated><entry m:etag=""61""><id>Web/Lists(guid\'5344b09b-d3f7-44ec-bd52-a8f5a99f23f9\')/Items(23)</id><category term="SP.Data.StationsListItem" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" /><link rel="edit" href="Web/Lists(guid\'5344b09b-d3f7-44ec-bd52-a8f5a99f23f9\')/Items(23)" /><title /><updated>2017-03-20T08:15:47Z</updated><author><name /></author><content type="application/xml"><m:properties><d:OfficeId>KWIKU08AA</d:OfficeId></m:properties></content></entry></feed>';
echo explode("</d:OfficeId>",explode("<d:OfficeId>", $string)[1])[0];
PHP Code demo

get xml of gestis database

i try to get the (not xml aparently) content of this website:
http://gestis.itrust.de/nxt/gateway.dll/gestis_de/010520.xml?f=templates$fn=default-doc.htm$3.0
via curl or file_get_contents in php.
you can open the website in any browser but whenever i try to open it with php to get the content automated it will return a 500 error.
here is the code used:
<?php
/* gets the data from a URL */
function get_data($url) {
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$returned_content = get_data('http://gestis.itrust.de/nxt/gateway.dll/gestis_de/010520.xml?f=templates$fn=default-doc.htm$3.0');
echo $returned_content;
?>
does anybody have an idea how to get the xml via php from this website?
The website you want to open needs the vid=gestisdeu:sdbdeu value in form of a cookie to work:
Cookie: nxt/gateway.dll/vid=gestisdeu%3Asdbdeu;
Please consult the curl documentation how you can set cookies or take a look into the existing material that is already on this webiste, for example Is it possible to set the cookie content with CURL? and the like.
Take care that depending on website and their configuration changes this might become different. So technically your question can't be really answered, because that website doesn't have any documentation of it's HTTP request requirements. So you need to find out on your own and provide those if you ask such a question.
PHP Example:
$url = 'http://gestis.itrust.de/nxt/gateway.dll/gestis_de/010520.xml?f=templates$fn=default-doc.htm$3.0';
$options['http'] = ['header' => 'Cookie: nxt/gateway.dll/vid=gestisdeu%3Asdbdeu;'];
stream_context_set_default($options);
$content = file_get_contents($url);
var_dump($content);
Output:
string(104975) "<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>DGUV-IFA GESTIS</title>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
</head>
<body>
<html>
<head>
<META http-equiv="Content-Type" content="text/html">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel="stylesheet" href="/nxt/gateway.dll/gestis_de/010520.xml?f=stylesheets$fn=gestis-doc.css$up=1$3.0" type="text/css">
<"...

gmail unread email count using curl

I created a script that can get the unread email list as a feed from the gmail here is my code
<?php
//function to get unread emails taking username and password as parameters
function check_email($username, $password)
{
//url to connect to
$url = "https://mail.google.com/mail/feed/atom";
// sendRequest
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_ENCODING, "");
$curlData = curl_exec($curl);
curl_close($curl);
//returning retrieved feed
return $curlData;
}
//making page to behave like xml document to show feeds
header('Content-Type:text/xml; charset=UTF-8');
//calling function
$feed = check_email("username", "password");
echo $feed;
?>
the output getting like this
<?xml version="1.0" encoding="UTF-8"?>
<feed version="0.3" xmlns="http://purl.org/atom/ns#">
<title>Gmail - Inbox for suneth2#gmail.com</title>
<tagline>New messages in your Gmail Inbox</tagline>
<fullcount>1282</fullcount>
<link rel="alternate" href="http://mail.google.com/mail" type="text/html" />
<modified>2012-08-01T12:33:48Z</modified>
<entry>
<title>eBCS Pro 1 August 2012</title>
<summary>bcs logo eBCS Pro 1 August 2012 Video interview Olympic IT The Met Police's director of IT, Steve</summary>
<link rel="alternate" href="http://mail.google.com/mail?account_id=test#gmail.com&message_id=138e21a3404cc7b2&view=conv&extsrc=atom" type="text/html" />
<modified>2012-08-01T12:12:44Z</modified>
<issued>2012-08-01T12:12:44Z</issued>
<id>tag:gmail.google.com,2004:1409100718455703474</id>
<author>
<name>eBCS Newsletter</name>
<email>e-bulletin#lists.bcs.org.uk</email>
</author>
</entry>
<entry>
so want to read the
<fullcount>1282</fullcount>
tag
when you pass the username and password to this function it can show the email list,
I need to get only the message count, is there any way to catch or count the items
this is the answer, i read the xml tag using php
$xmlobjc = new SimpleXMLElement($feed);
echo $xmlobjc->fullcount[0];
and replace the header with
header('Content-Type:text/html; charset=UTF-8');
This works too:
curl -u $(cat username):$(cat password) --silent 'https://mail.google.com/mail/feed/atom' | sed -n 's:.*<fullcount>\(.*\)</fullcount>.*:\1:p'

PHP GMAIL Contacts XML Parsing with DOMDocument and cURL

What I am trying to get currently is just the attribute of gd:email that is the "address=" only nothing else as of the moment. Which I can get to the xml portion, heck I can even get any given think per say as long as its within tags like but to get the attribute of any given one like in my case.. I am completely confused on. I used to know how to do it but its been so long since I did anything that wasn't simple for XML useage. so I done messed my own self up..
<?xml version='1.0' encoding='UTF-8'?>
<feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:gContact='http://schemas.google.com/contact/2008' xmlns:batch='http://schemas.google.com/gdata/batch' xmlns:gd='http://schemas.google.com/g/2005'>
<id>xxxxxxxxxxxxxx#gmail.com</id>
<updated>2011-06-30T00:07:48.706Z</updated>
<category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/contact/2008#contact'/>
<title type='text'>Taco Bells's Contacts</title>
<link rel='alternate' type='text/html' href='http://www.google.com/'/>
<link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://www.google.com/mate/feeds/contacts/xxxxxxxxxxxxxx%40gmail.com/full'/>
<link rel='http://schemas.google.com/g/2005#post' type='application/atom+xml' href='http://www.google.com/mate/feeds/contacts/xxxxxxxxxxxxxx%40gmail.com/full'/>
<link rel='http://schemas.google.com/g/2005#batch' type='application/atom+xml' href='http://www.google.com/mate/feeds/contacts/xxxxxxxxxxxxxx%40gmail.com/full/batch'/>
<link rel='self' type='application/atom+xml' href='http://www.google.com/mate/feeds/contacts/xxxxxxxxxxxxxx%40gmail.com/full?max-results=5'/>
<link rel='next' type='application/atom+xml' href='http://www.google.com/mate/feeds/contacts/xxxxxxxxxxxxxx%40gmail.com/full?start-index=6&max-results=5'/>
<author>
<name>Taco Bell</name>
<email>xxxxxxxxxxxxxx#gmail.com</email>
</author>
<generator version='1.0' uri='http://www.google.com/mate/feeds'>Contacts</generator>
<openSearch:totalResults>90</openSearch:totalResults>
<openSearch:startIndex>1</openSearch:startIndex>
<openSearch:itemsPerPage>5</openSearch:itemsPerPage>
<entry>
<id>http://www.google.com/mate/feeds/contacts/xxxxxxxxxxxxxx%40gmail.com/base/0</id>
<updated>2010-01-27T00:11:57.430Z</updated>
<category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/contact/2008#contact'/>
<title type='text'>Taco B</title>
<link rel='http://schemas.google.com/contacts/2008/rel#edit-photo' type='image/*' href='http://www.google.com/mate/feeds/photos/media/xxxxxxxxxxxxxx%40gmail.com/0/8_XL_JoAPon1k7SNixI2iA'/>
<link rel='http://schemas.google.com/contacts/2008/rel#photo' type='image/*' href='http://www.google.com/mate/feeds/photos/media/xxxxxxxxxxxxxx%40gmail.com/0'/>
<link rel='self' type='application/atom+xml' href='http://www.google.com/mate/feeds/contacts/xxxxxxxxxxxxxx%40gmail.com/full/0'/>
<link rel='edit' type='application/atom+xml' href='http://www.google.com/mate/feeds/contacts/xxxxxxxxxxxxxx%40gmail.com/full/0/1264551117430000'/>
<gd:email rel='http://schemas.google.com/g/2005#other' address='xxxxxxxxxxxxxx#gmail.com' primary='true'/>
</entry>
<entry>
<id>http://www.google.com/mate/feeds/contacts/xxxxxxxxxxxxxx%40gmail.com/base/1</id>
<updated>2007-08-01T18:02:04.410Z</updated>
<category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/contact/2008#contact'/>
<title type='text'></title>
<link rel='http://schemas.google.com/contacts/2008/rel#edit-photo' type='image/*' href='http://www.google.com/mate/feeds/photos/media/xxxxxxxxxxxxxx%40gmail.com/1/1B2M2Y8AsgTpgAmY7PhCfg'/>
<link rel='self' type='application/atom+xml' href='http://www.google.com/mate/feeds/contacts/xxxxxxxxxxxxxx%40gmail.com/full/1'/>
<link rel='edit' type='application/atom+xml' href='http://www.google.com/mate/feeds/contacts/xxxxxxxxxxxxxx%40gmail.com/full/1/1185991324410001'/>
<gd:email rel='http://schemas.google.com/g/2005#other' address='xxxxxxxxxxxxxx#sbcglobal.net' primary='true'/>
</entry>
<entry>
<id>http://www.google.com/mate/feeds/contacts/xxxxxxxxxxxxxx%40gmail.com/base/2</id>
<updated>2010-01-27T00:11:57.430Z</updated>
<category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/contact/2008#contact'/>
<title type='text'>Steve Sattler</title>
<link rel='http://schemas.google.com/contacts/2008/rel#edit-photo' type='image/*' href='http://www.google.com/mate/feeds/photos/media/xxxxxxxxxxxxxx%40gmail.com/2/1B2M2Y8AsgTpgAmY7PhCfg'/>
<link rel='self' type='application/atom+xml' href='http://www.google.com/mate/feeds/contacts/xxxxxxxxxxxxxx%40gmail.com/full/2'/>
<link rel='edit' type='application/atom+xml' href='http://www.google.com/mate/feeds/contacts/xxxxxxxxxxxxxx%40gmail.com/full/2/1264551117430000'/>
<gd:email rel='http://schemas.google.com/g/2005#other' address='xxxxxxxxxxxxxx#yahoo.com' primary='true'/>
</entry>
<entry>
<id>http://www.google.com/mate/feeds/contacts/xxxxxxxxxxxxxx%40gmail.com/base/3</id>
<updated>2010-01-27T00:11:57.430Z</updated>
<category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/contact/2008#contact'/>
<title type='text'>Michael Montana</title>
<link rel='http://schemas.google.com/contacts/2008/rel#edit-photo' type='image/*' href='http://www.google.com/mate/feeds/photos/media/xxxxxxxxxxxxxx%40gmail.com/3/1B2M2Y8AsgTpgAmY7PhCfg'/>
<link rel='self' type='application/atom+xml' href='http://www.google.com/mate/feeds/contacts/xxxxxxxxxxxxxx%40gmail.com/full/3'/>
<link rel='edit' type='application/atom+xml' href='http://www.google.com/mate/feeds/contacts/xxxxxxxxxxxxxx%40gmail.com/full/3/1264551117430000'/>
<gd:email rel='http://schemas.google.com/g/2005#other' address='xxxxxxxxxxxxxx#comcast.net' primary='true'/>
</entry>
<entry>
<id>http://www.google.com/mate/feeds/contacts/xxxxxxxxxxxxxx%40gmail.com/base/4</id>
<updated>2007-08-01T18:02:04.410Z</updated>
<category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/contact/2008#contact'/>
<title type='text'></title>
<link rel='http://schemas.google.com/contacts/2008/rel#edit-photo' type='image/*' href='http://www.google.com/mate/feeds/photos/media/xxxxxxxxxxxxxx%40gmail.com/4/1B2M2Y8AsgTpgAmY7PhCfg'/>
<link rel='self' type='application/atom+xml' href='http://www.google.com/mate/feeds/contacts/xxxxxxxxxxxxxx%40gmail.com/full/4'/>
<link rel='edit' type='application/atom+xml' href='http://www.google.com/mate/feeds/contacts/xxxxxxxxxxxxxx%40gmail.com/full/4/1185991324410001'/>
<gd:email rel='http://schemas.google.com/g/2005#other' address='xxxxxxxxxxxxxx#courant.com' primary='true'/>
</entry>
</feed>
with code that looks like (this is the whole script (currently))
$user = "xxxxxxxxxxxxxxxxxxx#gmail.com";
$password = "xxxxxxxxxxxx";
// ref: http://code.google.com/apis/accounts/docs/AuthForInstalledApps.html
// step 1: login
$login_url = "https://www.google.com/accounts/ClientLogin";
$fields = array(
'Email' => $user,
'Passwd' => $password,
'service' => 'cp', // <== contact list service code
'source' => 'test-google-contact-grabber',
'accountType' => 'GOOGLE',
);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL,$login_url);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS,$fields);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
$returns = array();
foreach (explode("\n",$result) as $line)
{
$line = trim($line);
if (!$line) continue;
list($k,$v) = explode("=",$line,2);
$returns[$k] = $v;
}
curl_close($curl);
// step 2: grab the contact list
$feed_url = "http://www.google.com/m8/feeds/contacts/$user/full?&max-results=5";
$header = array(
'Authorization: GoogleLogin auth=' . $returns['Auth'],
);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $feed_url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
curl_close($curl);
$doc = new DOMDocument();
$doc->load($result);
$arrFeeds = array();
foreach ($doc->getElementsByTagName('entry') as $node) {
$monkey = $node->getAttribute('{gd:email}');
$itemRSS = array($monkey->nodeValue);
array_push($arrFeeds, $itemRSS);
}
array_unique($arrFeeds);
Have a look at DOMDocument and DOMXPath. In order to get nodes that are part of a particular namespace, like the email node which is part of the gd namespace, you need to register the namespace with the DOMXPath object using DOMXPath::registerNamespace(). The namespace URI can usually be found at the top of the XML document.
Example:
$doc = new DOMDocument;
$doc->recover = true;
$doc->loadXML($result);
$xpath = new DOMXPath($doc);
$xpath->registerNamespace('gd', 'http://schemas.google.com/g/2005');
$emails = $xpath->query('//gd:email');
foreach ( $emails as $email )
{
echo $email->getAttribute('address');
// To get the title.
// This could also be done using XPath.
// You can also use ->nodeValue instead of ->textContent.
echo $email->parentNode->getElementsByTagName('title')->item(0)->textContent;
}
In the example above, $result is the result of $result = curl_exec($ch);.
$url = 'https://www.google.com/m8/feeds/contacts//full?max-results='.$max_results.'&oauth_token='.$accesstoken.'&alt=json&updated-min=2007-03-16T00:00:00';
function curl_file_get_contents($url)
{
$curl = curl_init();
$userAgent = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)';
//The URL to fetch. This can also be set when initializing a session with curl_init().
curl_setopt($curl,CURLOPT_URL,$url);
curl_setopt($curl, CURLOPT_HTTPHEADER,array('GData-Version: 2.0'));
curl_setopt($curl,CURLOPT_RETURNTRANSFER,TRUE); //TRUE to return the transfer as a string of the return value of curl_exec() instead of outputting it out directly.
curl_setopt($curl,CURLOPT_CONNECTTIMEOUT,5); //The number of seconds to wait while trying to connect.
curl_setopt($curl,CURLOPT_HTTPGET,true);
curl_setopt($curl, CURLOPT_USERAGENT, $userAgent); //The contents of the "User-Agent: " header to be used in a HTTP request.
//curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE); //To follow any "Location: " header that the server sends as part of the HTTP header.
//curl_setopt($curl, CURLOPT_AUTOREFERER, TRUE); //To automatically set the Referer: field in requests where it follows a Location: redirect.
curl_setopt($curl, CURLOPT_TIMEOUT, 10); //The maximum number of seconds to allow cURL functions to execute.
//curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); //To stop cURL from verifying the peer's certificate.
$contents = curl_exec($curl);
curl_close($curl);
return $contents;
}

Categories