imgur api won't work - php

i have a question about the imgur api.
I want to create a gallery for my website using the imgur api, but how can i create a file uploader that uploads to the imgur servers?
Here is what i created:
<?php
include 'xmlparser.php'; // From http://www.criticaldevelopment.net/xml/doc.php
if($_SERVER['REQUEST_METHOD'] == "POST"){
$data = file_get_contents($_FILES["file"]['tmp_name']);
// $data is file data
$pvars = array('image' => base64_encode($data), 'key' => HERE_MY_API_KEY);
$timeout = 30;
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'http://api.imgur.com/2/upload.xml');
curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $pvars);
$xml = curl_exec($curl);
$parser = new XMLParser($xml);
$parser->Parse();
echo $parser->images->item->links->original;
curl_close ($curl);
}
else
{
?>
<form action="test.php" method="post" enctype="multipart/form-data">
<input type="file" name="file" id="file" />
<input type="submit" name="submit" value="Submit" />
</form>
<?php
}
?>
But this doesn't seem to work...?
I get this error:
Parse error: syntax error, unexpected T_STRING, expecting ')' in C:\data\home\www\test.php on line 7
And line 7 is this row:
$pvars = array('image' => base64_encode($data), 'key' => HERE_MY_API_KEY);
What is wrong?
The documentation of the imgur api is here:
http://api.imgur.com/examples
Can you guys help me?
And yes, i already searched through these topics:
HTML Upload Form will only upload files found in the directory of the PHP file
Using jQuery to parse XML returned from PHP script (imgur.com API)
But it didn't help me...
Greetings

Put the API key in quote marks. The way they put it in all caps and outside quote marks is to signify a constant value.

Make sure you register for anonymous API and not Authenticated API here http://imgur.com/register/api
Pulled my hair off for this

Related

Sending a file to a webservice with XML using cURL corrupts it

So I have some code that sends a career application to a webservice which then emails the file to the hr department as an attachment along with other elements in the form. All of the parts except for the file gets emailed as desired. The file gets uploaded to the server without being corrupted. But the resulting email attachment ends up being corrupted.
The problem is; the file ends abruptly before reaching EOF. Let's say it is a pdf file when I open both the original and the reduced size file in a text editor I see that the beginnings are identical until one of them suddenly ends. One of them is about 1MB and the corrupt one is about 600kB.
I have tried sending files smaller(4kB) than the resulting corrupt file but that file also gets corrupt in the same way. The resulting file is about 1kB.
The xml response I get says:
<?xml version="1.0" encoding="utf-8"?><SENDEMLRSP><RTCD>1</RTCD><EXP>OK</EXP><RSP_LIST><RSP><MSGID>0</MSGID><EID /><RESULT>Invalid length for a Base-64 char array or string.</RESULT></RSP></RSP_LIST></SENDEMLRSP>
It is this part that is of interest:
<RESULT>Invalid length for a Base-64 char array or string.</RESULT>
I have prepared a small form with only a file upload for testing purposes.
Here is the HTML:
<html>
<body>
<form action="upload_file.php" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
Here is the relevant PHP code:
if(isset($_FILES['file']['name']))
{
echo ($_FILES['file']['name']);
echo ($_FILES['file']['tmp_name']);
$target = $_FILES['file']['name'];
move_uploaded_file( $_FILES['file']['tmp_name'], $target);
$rawdata = file_get_contents($target);
$data = urldecode($rawdata);
$data = base64_encode($rawdata);
//error_log('uploadconvertscope');
$iletisimrcpt = '<RCPT>
<TA>someemail#address.com</TA>
<MSG>kgsg</MSG>
<SBJ>'. strlen($rawdata).'</SBJ>
<OBOE>'.OBOE.'</OBOE>
<OBON>'.OBON.'</OBON>
<ATT_LIST><ATT><FN>'.$_FILES['file']['name'].'</FN><DATA>'.$data.'</DATA></ATT></ATT_LIST>
</RCPT>';
$request = '<?xml version="1.0" encoding="utf-8"?>
<SENDEML>
<VERSION>1.0</VERSION>
<TOKEN>'.$token.'</TOKEN>
<JID>'.JOBID.'</JID>
<MSG>Kariyar Basvuru isteði baþarýyla yerleþtirildi.</MSG>
<SBJ>Kariyar Basvuru</SBJ>
<RCPT_LIST>
'.$iletisimrcpt.'
</RCPT_LIST>
</SENDEML>';
error_log($request );
$params = array('data' => $request);
$response = processRequest(EML_URL, $params);
error_log($response );
$xml = new SimpleXmlElement($response);
}
The processRequest function works with the rest of the message. So it may not be the problem but here is the code:
<?php
function processRequest($url, $params) {
if(!is_array($params))
return false;
$post_params = "";
foreach($params as $key => $val) {
$post_params .= $post_params?"&":"";
$post_params .= $key."=".$val;
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 0);
curl_setopt($ch, CURLOPT_HEADER, false); // 'true', for developer testing purpose
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_params);
$data = curl_exec($ch);
if(curl_errno($ch))
print curl_error($ch);
else
curl_close($ch);
return $data;
}
?>
The file when read as a string between the looks like AHFAY3453GAW//LONG RANDOM STRING OF CHARACTERS//== it always ends with two "==" signs if that means anything.
I am really stumped as the files get uploaded OK with this C# code:
byte[] attach1 = File.ReadAllBytes(#"C:\Users\user\Downloads\amb.pdf");
string attach = Convert.ToBase64String(attach1);
EmlRequest.SetConnectionInformation("someapi.com", "admin", "password");
EmlRequest eml=new EmlRequest(){ MessageJobId="DASFA1SDFAWEFA4X2==" };
eml.Recipients.Add(new ApiEmlRecipient() { TargetAddress = "email#address.com" ,ToName="name",Message="xxx",Subject="subject"});
eml.Recipients[0].Attachments.Add(new ApiEmlAttachment() { FileName = "abm.pdf", Data = attach });
eml.Send();
Which is almost identical to it's PHP version.
I figured this out quite some time ago but only got down to writing an answer.
So the problem was that the "+" signs in the string were replaced by spaces.
This bit was responsible:
$rawdata = file_get_contents($target);
$data = urldecode($rawdata);
$data = base64_encode($rawdata);
I changed it to this:
$rawdata = file_get_contents($_FILES['uploadedfile']['tmp_name']);
$data = base64_encode($rawdata);
$data = urlencode($data);
Now it works.

XML parsing Imgur API issues

I'm trying to create a website that allows you to upload images to Imgur and stores the url for the image in the database for the site. To that end, I took a lot of code from this Stack overflow post: imgur api won't work , then modified it to fit my code, which is below:
First: my form
<form action="uploadfile.php" method="post" enctype="multipart/form-data">
<input type="file" name="file" id="file" />
<input type="submit" name="submit" value="Submit" />
</form>
uploadfile.php:
<?
//configuration
define("IMGUR_API_KEY", "");
require("../includes/config.php");
include 'xmlparser.php'; // From http://www.criticaldevelopment.net/xml/doc.php
if($_SERVER['REQUEST_METHOD'] == "POST")
{
$data = file_get_contents($_FILES["file"]['tmp_name']);
// $data is file data
$pvars = array('image' => base64_encode($data), 'key' => IMGUR_API_KEY);
$timeout = 30;
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'http://api.imgur.com/2/upload.xml');
curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $pvars);
$xml = curl_exec($curl);
$parser = new XMLParser($xml);
$parser->Parse();
echo $parser->images->item->links->original;
curl_close ($curl);
}
else
{
render("uploadfile_form.php", ["title"=>"Upload Image"]);
}
?>
The configuration and render stuff are just a part of the framework I'm building my site on; just allows my program to run. However when my program does run and it hits line 27, which is this:
echo $parser->images->item->links->original;
I get a "Notice: Undefined property: XMLParser::$images in /home/jharvard/vhosts/project/html/uploadfile.php on line 26" and then three more notices for trying to get the property of a non-object on the same line. Any idea what's going wrong with $parser?

PHP Curl upload file with file browse button

I want to remote upload my file into a server that accept uploads
using curl , but without typing the full file path every time with "#" symbol
can i make a browse button to select files then proceed to curl upload
this is my code ::
$post = array("file_box"=>"#/path/to/myfile.jpg");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://remote-site");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$output = curl_exec($ch);
curl_close($ch);
return $output;
just want to change "#/path/to/myfile.jpg" by browse button which passes it's value to php variable
I want to change this [[ $post = array("file_box"=>"#/path/to/myfile.jpg"); ]]
to something like that
[[ $post = array("file_box"=>"#".$variable_contains_file_path_from_browse_button); ]]
to prevent upload the file to middle server(host this script) in the temp path just from the client to the remote server directly
is there any solutions around this
thanks all for any help.
There are several questions in SO which deals with this
here are some of the links
uploading files using curl
Curl php file upload
And also google search strategy is http://bit.ly/PMUf2b
But still since you are asking for a browse button upload from front end, here is the complete set along with the tutor link
you can follow the tutor here which has both front end and php code
upload using curl
Here is the php code
$request_url = ‘http://www.example.com/test.php’;
$post_params['name'] = urlencode(’Test User’);
$post_params['file'] = ‘#’.'demo/testfile.txt’;
$post_params['submit'] = urlencode(’submit’);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $request_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_params);
$result = curl_exec($ch);
curl_close($ch);
here is the html code
<form method=”post” action=”test.php” enctype=”multipart/form-data”>
<input type=”text” name=”name” value=”Test User” />
<input type=”file” name=”file” />
<input type=”submit” name=”submit” value=”submit” />
</form>

PHP cURL setup to send to remote host

I have a few elementary questions about cURL that I can't find answered in the cURL docs (probably because they are obvious to everyone else...). I have a file type input in a form that needs to send that file to a remote server. Does the cURL code go on the page with the form, or is the cURL on the page that the form sends you to, then it gets sent to the remote server?
Here is the form html:
<form action="send.php" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>
The cURL php I have so far which I don't even know if it's correct for what I'm trying to do, or if this goes on the same page or the send.php file the form goes to:
$ch = curl_init("http://remoteServer/upload_file.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CUROPT_POSTFIELDS, array('fileupload' => '#'.$_FILES['theFile'] ['tmp_name']));
echo curl_exec($ch);`
And on the remote server I have this file to receive it:
$folder = "files/";
$path = $folder . basename( $_FILES['file']['name']);
if(move_uploaded_file($_FILES['file']['tmp_name'], $path)) {
echo "The file ". basename( $_FILES['file']['name']). " has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
Is this even remotely close?
you don't need curl to upload a file from a browser, you can submit the form directly to the remote server to upload - just make sure the form submits to whatever file has that third block of code
Try this
$curlPost = array('fileupload' => '#'.$_FILES['theFile'] ['tmp_name']);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://remoteServer/upload_file.php');
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
$data = curl_exec();
curl_close($ch);
Another Example
How to upload image file to remote server with PHP cURL
http://www.maheshchari.com/upload-image-file-to-remote-server-with-php-curl/

How to implement imgur api (image host) on a website?

I came across http://api.imgur.com and thought that would be a usefull tool to use on my website. I then noticed that StackOwerflow uses it too, so it must be good )))
Although I'm struggling when I try to implement it. I took a look at
http://api.imgur.com/examples the PHP section, but it didn't help me much.
What I'm interested in is includin imgur api on my website so users can upload their images. I would than need to store img url/path so I can display it on a website.
e.g. have a form that will allow users to upload photo, then store url/path of the uploaded image in a database (VARCHAR).
Has anyone had a success with this system and could help me understand how to implement it like StackOwerflow uses it (only store image url in database not post).
Code I tried:
<form enctype="multipart/form-data" method="post" action="upload_img.php">
Choose your file here:
<input name="uploaded_file" type="file"/>
<input type="submit" value="Upload It"/>
</form>
upload_img.php
<?
$filename = "image.jpg";
$handle = fopen($filename, "r");
$data = fread($handle, filesize($filename));
// $data is file data
$pvars = array('image' => base64_encode($data), 'key' => IMGUR_API_KEY);
$timeout = 30;
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'http://api.imgur.com/2/upload.xml');
curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $pvars);
$xml = curl_exec($curl);
curl_close ($curl);
?>
I see that you have copy and pasted the example from the imgur API site, which gives the example of a filename which is contained within $filename. You need to point this variable at the file that PHP has uploaded.
Modify the $filename part of your script:
$filename = $_FILES['uploaded_file']['tmp_name'];
Source: POST method file uploading

Categories