JIRA API attachment names contain the whole paths of the posted files - php

I have been working with Jira API and have seen inconsistent results for my requests. Sometimes it works and sometimes it doesn't. Last week I was able to post attachments to issues just fine, but now an old problem occurred: the names of the attachments contain the whole path of the posted file, hence the attachments can't be opened. I use json representation to post files:
$array = array("file"=>"#filename");
json_encode($array);
...
This gets the file posted but the problem is when it's posted the file names in JIRA are like this:
/var/www/user/text.text
Needless to say it can't be opened in JIRA. I had this problem before, then it suddenly disappeared, now it occurred again. I don't really get it. By the way I am not using curl for this request even though it might be recommended in the documentation.

I realize this question is somewhat old but I had a similar problem. It seems Jira doesn't necessarily trim the filename as expected. I was able to fix it with the following. If you're using PHP >= 5.5.0:
$url = "http://example.com/jira/rest/api/2/issue/123456/attachments";
$headers = array("X-Atlassian-Token: nocheck");
$attachmentPath = "/full/path/to/file";
$filename = array_pop(explode('/', $attachmentPath));
$cfile = new CURLFile($attachmentPath);
$cfile->setPostFilename($filename);
$data = array('file'=>$cfile);
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, "$user:$pass");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$result = curl_exec($ch);
$ch_error = curl_error($ch);
if ($ch_error){
echo "cURL Error: $ch_error"; exit();
} else {
print_r($result);
}
For PHP <5.5.0 but > 5.2.10 (see this bug):
$data = array('file'=>"#{$attachmentPath};filename={$filename}");

Yes, I filed an issue on this at https://jira.atlassian.com/browse/JRA-30765
Adding attachments to JIRA by REST is sadly not as useful as it could be.
Interesting that the problem went away - perhaps you were running your script from a different location?

Related

cURL PHP script work everywhere except on iPhoneq

I've been struggling since 1 month trying to find how every browser, every mobile phone except iPhone, can accept my cURL URL request but not on iPhone?
I mean, when I go to URL to call the cURL (via the URL which is a PHP file, it always give me an empty query).
I tried every possible way, user-agent, header, SSL, with cURL option and nothing working.
Every browser working and also every Samsung phone, why does iPhone ignore it?
I also tried it with Safari that I've downloaded for Windows, and work well.
Anybody already had these issues?
I just don't know what to do, there nothing to help me find the problem, no tools... :(
Here is the cURL call (this is a function but you got it how i call it):
private function __request($url, $post = false, array $headers = NULL, $decode_json = true){
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
if($post){
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, is_array($post) ? http_build_query($post) : $post);
}
if($headers){
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
}
$data = curl_exec($ch);
if($decode_json){
return json_decode($data);
} else {
return $data;
}
}
Thanks for any help

Use PHP Library

I have found the following PHP library which seems to fit my needs for comparing HTML: https://github.com/caxy/php-htmldiff
Unfortunately I do not have any clue how to wrap this thing into my one of my projects. This is what I tried:
include('./HtmlDiff.php');
if(isset($_GET['old']) && $_GET['new']){
$oldUrl = $_GET['old'];
$newUrl = $_GET['new'];
$oldContent = getHttpsContent($oldUrl);
$newContent = getHttpsContent($newUrl);
$diff = new HtmlDiff( $oldContent, $newContent );
echo $diff->build();
}else{
echo "Please provide an old and new GET-Parameter containing the URLs of the pages to be compared.";
}
function getHttpsContent($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_REFERER, $url);
curl_setopt($ch, CURLOPT_POST, 0);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
I get the following error:
Fatal error: Class 'Caxy\HtmlDiff\AbstractDiff' not found in Caxy\HtmlDiff\HtmlDiff.php on line 10
I suppose this is caused by the fact that it uses namespaces and I think that this could be resolved by using composer. At least thats the only thing I saw mentioned in the installation instructions, since I have no clue about inclusion of PHP libraries myself. I looked at the Composer site and downloaded it but it seems to be the case that you have to install it on the dev-server to use it, which is not possible for me.
Can someone please assist me on using this library?

Adding events via places return 404 error:

Please excuse my terminology if I get anything wrong, I'm new to Google API
I'm trying to add events via the Places API, for a single venue (listed under the bar category). I've followed the instructions here:
https://developers.google.com/places/documentation/actions?utm_source=welovemapsdevelopers&utm_campaign=places-events-screencast#event_intro
and this is the URL I am posting to (via PHP)
https://maps.googleapis.com/maps/api/place/event/add/format?sensor=false&key=AIzaSyAFd-ivmfNRDanJ40pWsgP1 (key altered)
which returns a 404 error. If I have understood correctly, I have set the sensor to false as I am not mobile, and created an API key in Google apis, with the PLACES service turned on.
Have I missed a vital step here, or would a subsequent error in the POST submission cause a 404 error? I can paste my code in but I thought I'd start with the basics.
Many thanks for your help, advice and time. It's very much apprecciated.
I've added this line to my CurlCall function which I believe should specify a POST, and the result is still the same.
curl_setopt($ch, CURLOPT_POST, 1);
so the whole functions reads
function CurlCall($url,$topost)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADERS, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $topost);
$body = curl_exec($ch);
curl_close($ch);
echo $body;
echo '<br>';
echo 'URL ' . $url;
echo '<br>';
return $body;
}
Are you sure that you are using the POST method and not GET?
You need to specify the format of your request in the URL by changing the word format to either json or xml depending on how you are going to structure and POST your request.
XML:
https://maps.googleapis.com/maps/api/place/event/add/xml?sensor=false&key=your_api_key
JSON:
https://maps.googleapis.com/maps/api/place/event/add/json?sensor=false&key=your_api_key

How can I upload a file that has spaces is its name with curl + php?

I'm using this code for uploading a file to my server, using HTTP POST:
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
$post = array(
"upload" => '#' . $filepath
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$curl_result = curl_exec($ch);
This code works when $filepath does not contain spaces. However, it is possible that it might. When I test a path with spaces, I get the curl error "failed creating formpost data".
The curl manual does not tell me what to do, all it gives me are unix filenames without spaces. I tried following http://curl.haxx.se/mail/archive-2006-01/0079.html like so, but it didn't help me either:
"upload" => '"#' . $filepath . '"'
"upload" => '#"' . $filepath . '"'
Anyone have an idea?
Current versions of cURL (and PHP-cURL) handle spaces in filenames with no problems. Older versions may have had bugs, I'm not sure. Check you're using reasonably recent versions.
I can confirm spaces in filenames are no problem at all under PHP 5.2.13 and libcurl/7.19.4 on Linux.
The error message you're getting is the same one as when PHP/cURL can't find a file. Odds are the problems are somewhere else in your code. Probably an issue of having under- or over-escaped the spaces in the filename but probably not an issue with cURL itself.
I'd suggest looking elsewhere in your code for the problem or producing a hard-coded example of the problem occurring (where $filename is being set in the PHP directly rather than read from elsewhere).
You need the realpath
$filepath = 'D:\7 habits copy.txt';
$url = 'http://www.speedyshare.com/upload.php?'.rand(10000000, 99999999);
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
$post = array(
"upload" => '#' . realpath($filepath)
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$curl_result = curl_exec($ch);
echo $curl_result;
Worked even on Windows. I'm pretty sure it will work on *NIX systems
I faced the same problem and I couldn't upgrade CURL on the system.
So I wrote a simple escape function that iterated on the string to replace the space character for the + sign, which is the traditional way to represent URLs with spaces.
For my surprise, it worked. :)
C++ code:
#include <algorithm>
#include <string>
std::string s = "";
std::replace(s.begin(), s.end(), ' ', '+');
You will need to escape the spaces. Not sure of the syntax you would use but maybe something like this:
'This is the file name with spaces.txt'
to
'This\ is\ the\ file\ name\ with\ spaces.txt'
again I'm not sure of the syntax it's just an example
or you might just need to add quotes to the file name, like this:
'This is the file name with spaces.txt'
Put the target file in a variable, such as 'while read', and pass the variable in the curl the command. Be sure the variable is in double quotes and curl will handle it.
For example:
curl -T /PATH/TO/YOUR/FILE/DIRECTORY/"$f"

Updating Twitter background via API

I'm having a little trouble updating backgrounds via Twitter's API.
$target_url = "http://www.google.com/logos/11th_birthday.gif";
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
curl_setopt($ch, CURLOPT_URL,$target_url);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$html = curl_exec($ch);
$content = $to->OAuthRequest('http://twitter.com/account/update_profile_background_image.xml', array('profile_background_image_url' => $html), 'POST');
When I try to pull the raw data via cURL or file_get_contents, I get this...
Expectation Failed The expectation given in the Expect request-header
field could not be met by this server.
The client sent
Expect: 100-continue but we only allow the 100-continue expectation.
OK, you can't direct Twitter to a URL, it won't accept that. Looking around a bit I've found that the best way is to download the image to the local server and then pass that over to Twitter almost like a form upload.
Try the following code, and let me know what you get.
// The URL from an external (or internal) server we want to grab
$url = 'http://www.google.com/logos/11th_birthday.gif';
// We need to grab the file name of this, unless you want to create your own
$filename = basename($url);
// This is where we'll be saving our new file to. Replace LOCALPATH with the path you would like to save the file to, i.e. www/home/content/my_directory/
$newfilename = 'LOCALPATH' . $filename;
// Copy it over, PHP will handle the overheads.
copy($url, $newfilename);
// Now it's OAuth time... fingers crossed!
$content = $to->OAuthRequest('http://twitter.com/account/update_profile_background_image.xml', array('profile_background_image_url' => $newfilename), 'POST');
// Echo something so you know it went through
print "done";
Well, given the error message, it sounds like you should load the URL's contents yourself, and post the data directly. Have you tried that?

Categories