It would be great if someone could help convert this function from Php to Perl, basically this function is meant to get contents of a https url. Right now I'm not good with Perl, I haven't learnt on how to use cURL just yet.
function curl_get_content($url)
{
$handle = curl_init();
curl_setopt($handle, CURLOPT_URL, $url);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($handle, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($handle, CURLOPT_SSLVERSION, 3);
$data = curl_exec($handle);
curl_close($handle);
return $data;
}
Thanks!
EDIT:
I attempted to use this
my $ua = LWP::UserAgent->new(ssl_opts => { verify_hostname => 1 });
my $header = HTTP::Request->new(GET => $URL);
my $request = HTTP::Request->new('GET', $URL, $header);
my $response = $ua->request($request);
if ($response->is_success){
print "URL:$URL\nHeaders:\n";
print $response->headers_as_string;
}elsif ($response->is_error){
print "Error:$URL\n";
print $response->error_as_HTML;
}
But it keeps giving me server error: 500.
My suspicion is that you're not setting the headers correctly so when the server is receiving your request and trying to process the headers, it's not understanding the content and just giving up (giving you the 500 error.) In your code, you're setting $header to a HTTP::Request object but that should actually be a HTTP::Headers object. Set that to:
my $header = HTTP::Headers->new();
$header->header( 'Content-Type' => 'text/plain' ); # set content-type to what you need
# you can add additional headers if needed
Related
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);
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);
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');
So I am getting this error and I have no clue why. When I run it on the server it's on, I get a 404 error with a simple request like this.
$json = file_get_contents('https://graph.facebook.com/me?access_token='.LONGSTRING);
The error is:
function.file-get-contents: failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found
However, I can paste the URL that I am using with file_get_contents directly in the browser at the same time and it comes up. So it seems as if Facebook is blocking my server.
Also it works half the time.
Any ideas?
Try cURL, not sure why file_get_contents is unreliable but I have seen the same issue in the past. I use this geturl function to cURL a URL and pass the parameters in as a PHP array
function geturl($url, $params) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params, null, '&'));
$ret = curl_exec($ch);
curl_close($ch);
return $ret;
}
Which can then be called like so
$url = 'https://www.graph.facebook.com/me';
$params = array('access_token' => '*********************');
$graph_ret = geturl($url, $params);
$json = #file_get_contents('https://graph.facebook.com/me?access_token='.$access_token) or die("ERROR");
use this will help you alot
I'm trying to get some basic information about a user in a PHP script (id and name).
I have tried the following methods:
$retrieve = curl_init("https://graph.facebook.com/me?access_token=$accesstoken");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($retrieve, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$data = curl_exec($retrieve);
curl_close($retrieve);
and
$user = json_decode(file_get_contents(
"https://graph.facebook.com/me?access_token=$accesstoken"))->me;
The cURL method (former) just times out. The file_get_contents (latter) method just doesn't return anything at all...
What could be causing this? Am I using cURL correctly?
for graph api you can use graph api methods rahter than curl
the following code grabs information of current user
define('FACEBOOK_APP_ID', 'Your API ID');
define('FACEBOOK_SECRET', 'YOUR SECRET');
function get_facebook_cookie($app_id, $application_secret)
{
$args = array();
parse_str(trim($_COOKIE['fbs_' . $app_id], '\\"'), $args);
ksort($args);
$payload = '';
foreach ($args as $key => $value)
{
if ($key != 'sig')
{
$payload .= $key . '=' . $value;
}
}
if (md5($payload . $application_secret) != $args['sig'])
{
return null;
}
return $args;
}
$cookie = get_facebook_cookie(FACEBOOK_APP_ID, FACEBOOK_SECRET);
$user=json_decode(file_get_contents('https://graph.facebook.com/me?access_token='.$cookie['access_token']));
its prettey easy
Facebook will not let you use curl. They have the api for that.
copy your link and paste it to browser. It will work. In Mozilla you will see the result in browser, IE will save the result as a file. So it is not about invalid access token etc. It is just because Facebook does not respond to your query when it does not come 1-from a web browser, 2-from Facebook APIs.
here is the relevant PHP call to Facebook.
$attachment = array('access_token' => $access_token);
$result=$facebook->api('/me', 'GET', $attachment);
$id = $result['id'];
$name=$result['name'];