Pulling metadate from url with php, strange characters? - metadata

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);

Related

Keycloak Rest API get all available resources

i am trying to list all resources client have access to. I am unable to figure out how to to make the call. I have used this curl
curl -X GET \
http://$URL/auth/realms/$RELM/authz/resource-server/resource \
-H 'Authorization: Bearer$TOKEN' \
-H 'cache-control: no-cache'
so far but i am getting this response :
{"error":"RESTEASY003210: Could not find resource for full path: http://localhost:8070/auth/realms/argo/authz/resource-server/resource"}
Can someone help me to figure out how i can list all resources and if resource is not in the list to create new one ?
SOLUTION that is implementd:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->keyCloakURL . '/realms/' . $this->relmName . '/protocol/openid-connect/token');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
"audience=" . KEYCLOAK_CLIENT_NAME . "&grant_type=urn:ietf:params:oauth:grant-type:uma-ticket&response_include_resource_name=true");
$authorization = "Authorization: Bearer " . $user_token['access_token'];
curl_setopt($ch, CURLOPT_HTTPHEADER, array (
'Content-Type: application/x-www-form-urlencoded',
$authorization
));
$result = curl_exec($ch);
if(curl_errno($ch))
{
echo 'curl error';
return false;
}
$result = json_decode($result, true);
curl_close($ch);
if(isset($result['access_token']) && !empty($result['access_token']))
{
$parts = explode('.', $result['access_token']);
if(!isset($parts[1]))
{
return false;
}
$info = $this->base64UrlDecode($parts[1]);
$info = json_decode($info, true);
$return = array ();
if(isset($info['authorization']['permissions']))
{
$permissions = $info['authorization']['permissions'];
foreach($permissions as $ecahPermission)
{
if(isset($ecahPermission['scopes']))
{
// $scopes = array_map('strtolower', $ecahPermission['scopes']);
$return[$ecahPermission['rsname']] = $ecahPermission['scopes'];
}
}
}
return $return;
}
return false;

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);

Paypal IPN CURL returns "SSL connect error"

I am trying to implement ipn in my system but shows error
SSL connect error
TLS version shows TLS 1.0
It is a client's system. I downloaded the code from paypal website. I am currently testing it.
but it throws error
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
session_start();
$ch = curl_init('https://www.howsmyssl.com/a/check');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
curl_close($ch);
$json = json_decode($data);
echo $json->tls_version;
file_put_contents('NAB/paypal-ipn.txt', var_export($_POST, true));
echo "ipn processing";
// STEP 1: read POST data
// Reading POSTed data directly from $_POST causes serialization issues with array data in the POST.
// Instead, read raw POST data from the input stream.
$raw_post_data = file_get_contents('php://input');
$raw_post_array = explode('&', $raw_post_data);
$myPost = array();
foreach ($raw_post_array as $keyval) {
$keyval = explode ('=', $keyval);
if (count($keyval) == 2)
$myPost[$keyval[0]] = urldecode($keyval[1]);
}
echo "<br>1";
// read the IPN message sent from PayPal and prepend 'cmd=_notify-validate'
$req = 'cmd=_notify-validate';
if (function_exists('get_magic_quotes_gpc')) {
$get_magic_quotes_exists = true;
}
echo "<br>2";
foreach ($myPost as $key => $value) {
if ($get_magic_quotes_exists == true && get_magic_quotes_gpc() == 1) {
$value = urlencode(stripslashes($value));
} else {
$value = urlencode($value);
}
$req .= "&$key=$value";
}
echo "<br>3";
// Step 2: POST IPN data back to PayPal to validate
$ch = curl_init('https://www.sandbox.paypal.com/cgi-bin/webscr');
//$ch = curl_init('https://www.paypal.com/cgi-bin/webscr');
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $req);
curl_setopt($ch, CURLOPT_SSLVERSION, 6);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: Close'));
// In wamp-like environments that do not come bundled with root authority certificates,
// please download 'cacert.pem' from "http://curl.haxx.se/docs/caextract.html" and set
// the directory path of the certificate as shown below:
// curl_setopt($ch, CURLOPT_CAINFO, dirname(__FILE__) . '/cacert.pem');
echo "<br>3";
if ( !($res = curl_exec($ch)) ) {
error_log("Got " . curl_error($ch) . " when processing IPN data");
echo curl_error($ch);
curl_close($ch);
//exit;
}
If 'howsmyssl' is reporting that the systems supports only TLS v 1.0, it is likely that an upgrade is necessary to support TLS v 1.2. The curl_setopt($ch, CURLOPT_SSLVERSION, 6); is requesting TLS v 1.2 (see list in PHP Curl (with NSS) is probably using SSLv3 insted of TLS when connecting to https).
Also, as of June 30,2017, PayPal will be requiring TLS v 1.2 (see https://www.paypal-knowledge.com/infocenter/index?page=content&widgetview=true&id=FAQ1914&viewlocale=en_US and Php curl set ssl version).
You may be able to test your integration using TLS v 1.0 (until 6/30/2017) by changing to curl_setopt($ch, CURLOPT_SSLVERSION, 4);.
I had a similar problem problem when implementing the paypal ipn listener code. The script would stop executing where step 2 starts in the code you've provided. I solved it by downloading cacert.pem from http://curl.haxx.se/docs/caextract.html. Then I copied the downloaded file to the same folder as paypalipnlistener.php and then uncommented the following code in paypalipnlistener.php // curl_setopt($ch, CURLOPT_CAINFO, dirname(__FILE__) . '/cacert.pem'); to curl_setopt($ch, CURLOPT_CAINFO, dirname(__FILE__) . '/cacert.pem'); and the script executed successfully.

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');