xpath dom preg_match - dom

I have this code with google map coordinates inside and i try to get coordinates but somewhere is a problem; The code is:
<iframe width="430" scrolling="no" height="250" frameborder="0" src="http://maps.google.cz/maps/ms?msa=0&hl=cs&brcurrent=5,0,0&ie=UTF8&vpsrc=6&msid=207589766138527801127.0004aadb2c99231cecabd&ll=44.782627,20.48152&spn=0.003808,0.009205&z=16&output=embed" marginwidth="0" marginheight="0"/>
and i try to get coordinates with this code:
$string = curl($e->textContent);
preg_match('#&ll=(.*?)&#is', $string, $matches);
list($lat, $lng) = explode(',', $matches[1]);
$data['lat'] = $lat;
$data['lng'] = $lng;
but dont work!
WHERE IS THE PROBLEM? WHERE I WRONG! (sorry for english)
This is my full code, but dont work:
function curl($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_ENCODING, "gzip");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
libxml_use_internal_errors(true);
$dom = new DOMDocument();
#$dom->loadHTMLFile('http://www.kupoman.rs/aktivne-ponude/');
$xpath = new DOMXPath($dom);
$entries = $xpath->query("//div[#class='dealcontent']/h1/a/#href");
$output = array();
$i = 1;
foreach($entries as $e) {
$dom2 = new DOMDocument();
#$dom2->loadHTMLFile($e->textContent);
$xpath2 = new DOMXPath($dom2);
$data = array();
$string = $xpath2->query("//iframe/#src")->item(0)->textContent;
$data['link']= ($e->textContent);
$data['naslov'] = trim($xpath2->query("//div[#class='dealcontent']/h1")->item(0)->textContent);
$data['slika'] = trim($xpath2->query("//div[#class='slideshow']/img[1]/#src")->item(0)->textContent);
preg_match('/.*&ll=([\d.,]+)&.*/', $string, $matches);
list($lat, $lng) = explode(',', $matches[1]);
$data['lat'] = $lat;
$data['lng'] = $lng;
all is good but coordinates is 0,0 :(

There's a lot wrong. There's no xpath expression, your regex is wrong and you are calling curl for some reason. Maybe you're trying to do something like this:
$dom = new DOMDocument();
#$dom->loadHTML('<iframe width="430" scrolling="no" height="250" frameborder="0" src="http://maps.google.cz/maps/ms?msa=0&hl=cs&brcurrent=5,0,0&ie=UTF8&vpsrc=6&msid=207589766138527801127.0004aadb2c99231cecabd&ll=44.782627,20.48152&spn=0.003808,0.009205&z=16&output=embed" marginwidth="0" marginheight="0"/>');
$xpath = new DOMXPath($dom);
$string = $xpath->query("//iframe/#src")->item(0)->textContent;
preg_match('/.*&ll=([\d.,]+)&.*/', $string, $matches);
list($lat, $lng) = explode(',', $matches[1]);
$data['lat'] = $lat;
$data['lng'] = $lng;

Related

How do we attach multiple images with JIRA rest attachments api via PHP Curl?

I am able to attach a single image via Jira Rest Api but it fails when i attempt to send multiple images through it. This is my code for single attachment. Need help to make multiple attachments work.
Reference:
Jira attach file to issue with PHP and CURL
$cfile = new CURLFile($attachment['tmp_name'],$attachment['type'], $attachment['name']);
$data = array('file' => $cfile);
$url = "{$uriapi}"."issue/"."{$bugid}"."/attachments";
curl_setopt_array(
$ch,
array(
CURLOPT_URL=>$url,
CURLOPT_POST=>true,
CURLOPT_VERBOSE=>1,
CURLOPT_POSTFIELDS=>$data,
CURLOPT_INFILESIZE => 10,
CURLOPT_SSL_VERIFYHOST=> 0,
CURLOPT_SSL_VERIFYPEER=> 0,
CURLOPT_RETURNTRANSFER=>true,
CURLOPT_HTTPHEADER=> $headers,
CURLOPT_USERPWD=>"$Jirausername:$Jirapassword"
)
);
$result=curl_exec($ch);
$ch_error = curl_error($ch);
if ($ch_error) {
echo "cURL Error: $ch_error";
return "Error Opening file. Failed to add Attachment";
}
elseif(isset($result)){
//return "Attachment added";
return "";
}
else{
return "Failed to add Attachment";
}
curl_close($ch);
}
The following code works fine for me, I hope it helps.
$username = "xxxxx";
$password = "xxxxx";
$url = "https://YourUrl/rest/api/latest/issue/YourKey/attachments";
$attachments = array("attachment1", "attachment2", "attachment3");
$curl = curl_init();
for ($i = 0; $i < count($attachments); $i++) {
$attachmentPath = "/your/attachment/path/$attachments[$i]";
$filename = array_pop(explode('/', $attachmentPath));
$cfile = new CURLFile($attachmentPath);
$cfile->setPostFilename($filename);
$data = array('file' => $cfile);
$headers = array(
'Content-Type: multipart/form-data',
'X-Atlassian-Token: nocheck'
);
curl_setopt($curl, CURLOPT_USERPWD, "$username:$password");
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_VERBOSE, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
$result = curl_exec($curl);
$ch_error = curl_error($curl);
if ($ch_error) {
echo "cURL Error: $ch_error";
} else {
echo $result;
}
}
curl_close($curl);

Need to get element from url

I have a url that looks like this
<span class="price" itemprop="lowPrice">14,70 €</span>
and i want to get the 14,70 € from this .
I have this
$url = "http://www.skroutz.gr/s/292655/Kingston-1GB-SODIMM-DDR3-Non-ECC-CL9-1333MHz.html";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
$output = curl_exec($curl);
curl_close($curl);
$DOM = new DOMDocument();
#$DOM->loadHTML( $output);
//get all
$items = $DOM->getElementsByTagName('li');
//display all
for ($i = 0; $i < $items->length; $i++)
echo $items->item($i)->nodeValue . "<br/>";
but i get everything tagged with li . Is there a way just to get the 14,70 € ?
Thank you in advance for your help
Assuming you just want to extract the price from $span where $span =
<span class="price" itemprop="lowPrice">14,70 €</span>
A couple of preg_replace() calls will get it:
# remove span tag from beginning of string
$span = preg_replace('/^<span[^>]*>/', '', $span) ;
#remove span tag from end of string
$span = preg_replace('/<\/span.*$/', '', $span) ;

Pulling metadate from url with php, strange characters?

I'm having a bit of an issue, im trying to pull basic metadata from an external URL, I have successfuly got it to do so for the most part but its causing a few character issues on letters that are Ä ä ö are coming out like mäenjaksa7-300x200.jpg when i call the images url which is actually mäenjaksa7-300x200.jpg, my code is below and thank you for helping.
function file_get_contents_curl($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$data = curl_exec($ch);
curl_close($ch);
return $data; }
$html = file_get_contents_curl($params['url']);
//parsing begins here:
$doc = new DOMDocument();
#$doc->loadHTML($html);
$nodes = $doc->getElementsByTagName('title');
//get and display what you need:
$urltitle = $nodes->item(0)->nodeValue;
$metas = $doc->getElementsByTagName('meta');
for ($i = 0; $i < $metas->length; $i++)
{
$meta = $metas->item($i);
if($meta->getAttribute('name') == 'description')
$description = $meta->getAttribute('content');
if($meta->getAttribute('name') == 'keywords')
$keywords = $meta->getAttribute('content');
if($meta->getAttribute('property') == 'og:image')
$ogimage = $meta->getAttribute('content');
if($meta->getAttribute('rel') == 'image_src')
$relimage = $meta->getAttribute('content');
}
if( empty($ogimage) ) {
$metaimage = $relimage;
} else {
$metaimage = $ogimage;
}
Perhaps you have to make sure that your url header have content-type -> charset to utf-8 or appropriate one. You have to make sure that your url is not content none Ascii character or make sure you have properly set the appropriate "character’s encoder". Maybe i haven’t well understood your problem, however look at this example which have not relation to your code but can be useful:
$url = "http://www.example.com/services/calculation";
$page = "/services/calculation";
$headers = array(
"POST ".$page." HTTP/1.0",
"Content-type: text/xml;charset=\"utf-8\"",
"Accept: text/xml",
"Cache-Control: no-cache",
"Pragma: no-cache",
"SOAPAction: \"run\"",
"Content-length: ".strlen($xml_data),
"Authorization: Basic " . base64_encode($credentials)
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_USERAGENT, $defined_vars['HTTP_USER_AGENT']);
Solution:
add this below
Find:
$html = file_get_contents_curl($url);
Add beow it:
//Change encoding to UTF-8 from ISO-8859-1
$html = iconv('UTF-8', 'ISO-8859-1//TRANSLIT', $html);

How to make individual share buttons for facebook, twitter and google plus

Like this:
See exmaple under the article:
http://www.golem.de/news/server-prozessor-intel-bestaetigt-broadwell-als-xeon-fuer-sockel-1150-1311-102622.html
The design is not the problem, but how to get the number of shares for this url for twitter, facebook and google plus?
Found the solution. Here is how to get the counts by API:
<?php
if ($network == 'facebook')
{
$string = file_get_contents('http://graph.facebook.com/?id=' . $current_url);
$array = json_decode($string, true);
if (isset($array['shares']))
$number = intval($array['shares']);
else
$number = 0;
}
elseif ($network == 'twitter')
{
$string = file_get_contents('http://urls.api.twitter.com/1/urls/count.json?url=' . $current_url);
$array = json_decode($string, true);
if (isset($array['count']))
$number = intval($array['count']);
else
$number = 0;
}
elseif ($network == 'googleplus')
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "https://clients6.google.com/rpc");
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_POSTFIELDS, '[{"method":"pos.plusones.get","id":"p","params":{"nolog":true,"id":"'.$current_url.'","source":"widget","userId":"#viewer","groupId":"#self"},"jsonrpc":"2.0","key":"p","apiVersion":"v1"}]');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
$curl_results = curl_exec($curl);
curl_close ($curl);
$array = json_decode($curl_results, true);
if (isset($array[0]['result']['metadata']['globalCounts']['count']))
$number = intval($array[0]['result']['metadata']['globalCounts']['count']);
else
$number = 0;
}
?>

check if facebook URL redirected or not?

i want to check if the url's in my database are reaching the facebook page they should or redirected to "www.facebook.com".
this is the code i use:
<?php
$conn = mysql_connect('localhost', 'user', 'pass');
mysql_select_db('database');
?>
<?php
$query = "SELECT data_txt FROM jos_sobi2_fields_data WHERE fieldid=8 ";
$result = mysql_query($query);
$row = mysql_fetch_array($result);
$url = $row['data_txt'];
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
foreach($row as $url) {
curl_setopt($ch, CURLOPT_URL, $url);
$out = curl_exec($ch);
$out = str_replace("\r", "", $out);
$headers_end = strpos($out, "\n\n");
if( $headers_end !== false ) {
$out = substr($out, 0, $headers_end);
}
$headers = explode("\n", $out);
foreach($headers as $header) {
if( substr($header, 0, 10) == "Location: " ) {
$target = substr($header, 10);
echo "[$url] redirects to [$target]<br>";
continue 2;
}
}
echo "[$url] does not redirect<br>";
}
?>
the result is this:
[http://www.facebook.com/shanibakshi.grooming.dogtraining] redirects to [http://www.facebook.com/common/browser.php]
[http://www.facebook.com/shanibakshi.grooming.dogtraining] redirects to [http://www.facebook.com/common/browser.php]
and this url -> http://www.facebook.com/common/browser.php is a facebook page that says my browser is old...probably because of some function in the code.....
anyway all i want to do is to check if the url in my database stays in their place with any redirection.
thanks :)
ronen.
Are you saying that you want to detect redirection, but the problem is you are always getting redirected to browser.php so you get nothing but "false positives"? In that case you probably just need to set the USERAGENT option, something like:
curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows NT 6.2; rv:9.0.1) Gecko/20100101 Firefox/9.0.1');