i'm trying to use imgur as a uploading backend - i guess it secure my website if there's upload picture (is it right?) so
i went with this way :
<?php
$client_id = 'xxxxx';
$file = file_get_contents($_FILES["imgupload"]["tmp_name"]);
$url = 'https://api.imgur.com/3/image.json';
$headers = array("Authorization: Client-ID $client_id");
$pvars = array('image' => base64_encode($file));
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL=> $url,
CURLOPT_TIMEOUT => 30,
CURLOPT_POST => 1,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_POSTFIELDS => $pvars
));
if ($error = curl_error($curl)) {
die('cURL error:'.$error);
}
$json_returned = curl_exec($curl); // blank response
echo "Result: " . $json_returned ;
curl_close ($curl);
?>
HTML form
<form action="upload" method="post" enctype="multipart/form-data">
<input type="file" name="imgupload" /><br>
<input type="submit" value="Upload to Imgur" />
</form>
when i click on submit the result is fine as i guess ^^
Result: {"data":{"id":"TvFtE29","title":null,"description":null,"datetime":1585015712,"type":"image\/png","animated":false,"width":900,"height":940,"size":48902,"views":0,"bandwidth":0,"vote":null,"favorite":false,"nsfw":null,"section":null,"account_url":null,"account_id":0,"is_ad":false,"in_most_viral":false,"has_sound":false,"tags":[],"ad_type":0,"ad_url":"","edited":"0","in_gallery":false,"deletehash":"Z9xFH8mrSH8lRDB","name":"","link":"https:\/\/i.imgur.com\/TvFtE29.png"},"success":true,"status":200}
but my problem i want to collect the https://i.imgur.com/TvFtE29.png into specific variable like $uploaded = 'https://i.imgur.com/TvFtE29.png'; to added into my database -> user pic
i went with json_decode but it's not completed with me in the right way, if someone can help with this issue 🌹
thanks.
json_decode worked fine here:
$json_returned = json_decode($json_returned, true);
$uploaded = $json_returned['data']['link'];
Related
I have to create an simple form:
<!DOCTYPE html>
<html lang="en">
<head>
<title>HTML page</title>
</head>
<body>
<form method="post" action="process.php">
<input type="text" name="firstname" placeholder="rahul_sharma">
<button type="submit">send</button>
</form>
</body>
</html>
From my process.php file, I have to hit an url like below:
https://stackoverflow.com/api?rahul_sharma
which will give back an json response
{"status":"Success","username":"your username is RAHULSHARMA"}
If status is success, have to display the username value.
New to php.Any help is appreciated.
you can call API using curl.
$url='https://stackoverflow.com/api?';
$call_url = $url . $_POST['first_name'] ;
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $call_url,
CURLOPT_SSL_VERIFYPEER => false,
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;//{"status":"Success","username":"your username is RAHULSHARMA"}
In your process.php file, you can use the $_POST superglobal to fetch the form data.
$firstname = $_POST['firstname'];
After that you can concatenate it using the . operator to form the url.
$url = "https://your-api-site.com/api?" . $firstname;
Next you can fetch the content from the url using a curl request.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
The result from fetching the url will be obtained in the $output variable. Suppose the result from your API is a JSON string like the one you provided, you can use json_decode PHP function to convert it to an associative array.
$result = json_decode($output);
Now you can use if conditions to check if status is Success and display the username.
if ($result['status'] == "Success") {
echo $result['username'];
}
This should be a pretty simple questions but I can't seam to find a simple answer. All of the questions I find deal with same jquery.
I have a php page that accepts for post data, places it in an array, passes the array to an api, and receives success/error from api.
I have an html page with a form. When I submit the form it passes the form data to the php file.
All I would like to do is return the success/error message's variable back to the html file. I don't care if the page reloads, I don't want any fancy features I'm just trying to do a simple test but have forgotten my php 101. any help or direction to references would be appreciated.
Html:
<div style="width: 400px; margin: 150px auto;">
<form action="api3.php" method="post">
<input type="text" placeholder="First Name" name="fname"><br><br>
<input type="text" placeholder="Last Name" name="lname"><br><br>
<input type="email" placeholder="Email" name="email"><br><br>
<input type="text" placeholder="Phone" name="phone"><br><br>
<select name="life"><br><br>
<option value="customer">Customer</option>
<option value="lead">Lead</option>
<option value="subscriber">Subsciber</option>
<option value="opportunity">Opportunity</option>
</select><br><br>
<input type="text" placeholder="Pizza" name="pizza"><br><br>
<input type="submit" value="Submit">
</form>
</div>
PHP:
<?php
$arr = array(
'properties' => array (
array(
'property' => 'email',
'value' => $_POST["email"]
),
array(
'property' => 'firstname',
'value' => $_POST["fname"]
),
array(
'property' => 'lastname',
'value' => $_POST["lname"]
),
array(
'property' => 'phone',
'value' => $_POST["phone"]
),
array(
"property" => "lifecyclestage",
"value" => $_POST["life"]
),
array(
"property" => "pizza",
"value" => $_POST["pizza"]
)
)
);
$post_json = json_encode($arr);
$hapikey = "/";
$endpoint1 = 'http://api.hubapi.com/contacts/v1/contact/createOrUpdate/email/' . $arr['properties'][0]['value'] . '/?hapikey=' . $hapikey;
$endpoint2 = 'http://api.hubapi.com/contacts/v1/lists/5/add?hapikey=' . $hapikey;
$ch = #curl_init();
#curl_setopt($ch, CURLOPT_POST, true);
#curl_setopt($ch, CURLOPT_POSTFIELDS, $post_json);
#curl_setopt($ch, CURLOPT_URL, $endpoint1);
#curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
#curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response1 = #curl_exec($ch);
$status_code1 = #curl_getinfo($ch, CURLINFO_HTTP_CODE);
$curl_errors1 = curl_error($ch);
if ($status_code1 == 200) {
$vid = json_decode($response1, true);
echo $vid['vid'] . '<br><br><br>';
$arr2 = array(
'vids' => array (
$vid['vid']
)
);
$vids_push = json_encode($arr2);
#curl_setopt($ch, CURLOPT_POSTFIELDS, $vids_push);
#curl_setopt($ch, CURLOPT_URL, $endpoint2);
$response2 = #curl_exec($ch);
$status_code2 = #curl_getinfo($ch, CURLINFO_HTTP_CODE);
$curl_errors2 = curl_error($ch);
#curl_close($ch);
return $response2;
}
?>
EDIT: I changed my form.html page to .php. I didn't want to share my code because it always seams to complicate things but all I want is to return $response2 back to my form.php page.
First of all the page you have form and want to get response should be with .php
Now for example, I have a page with form at www.example.com/work.php
//Your form here
<form> </form>
submit the form on other .php page that process input and get response from API.
at the end of page you have two methods to return data.
using GET
encode your variables in url and redirect page to work.php
$url = "www.example.com/work.php" + "?status=error&message=This is message";
header('Location: '.$url);
Now on work.php file you need to utilize these parameters we encoded with url using
echo $_GET['status'];
echo $_GET['message'];
// rest of the page will be same.
using SESSION
store variables in session and redirect to work.php without parameters
$_SESSION['status'] = "error";
$_SESSION['message'] = "This is message";
$url = "www.example.com/work.php";
header('Location: '.$url);
Again in work.php file display data from session and rest of code will be same.
Does anyone know how to upload a video to Wistia using PHP cURL from a file upload input specified within a form? This is my code but I cannot seem to get this to work with the current API. I’ve looked at a few similar posts on this subject but still no luck...
<?php
$pathToFile = $_FILES['fileName']['tmp_name']; /* /tmp/filename.mov */
$nameOfFile = $_FILES['fileName']['name']; /* filename.mov */
$data = array(
'api_password' => '********',
'file' => '#'.$pathToFile,
'project_id' => '********'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_URL, "https://upload.wistia.com" );
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$result = curl_exec($ch);
curl_close($ch);
?>
UPDATE - FULL CODE (still not working)
cURL returns false for both curl_exec and curl_error when using "'file' => '#'.$pathToFile" but works fine when using "'url' = 'http://example.com/file.mov'... Perhaps I'm not processing the form properly? Here's the full code:
<?php
if ($_POST['submit']) {
$filePath = $_FILES['fileUploaded']['tmp_name'];
$fileName = $_FILES['fileUploaded']['name'];
$fileSize = $_FILES['fileUploaded']['size'];
echo("File Path: ");
echo $filePath;
echo '<br>';
$data = array(
'api_password' => '******',
/* 'url' => 'http://example.com/file.mov', WORKS */
'file' => '#'.$filePath, /* DOES NOT WORK */
'project_id' => '******'
);
// WORKING CMD LINE
// curl -i -F api_password=****** -F file=#file.mov https://upload.wistia.com/
/* cURL */
$chss = curl_init('https://upload.wistia.com');
curl_setopt_array($chss, array(
CURLOPT_POST => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_POSTFIELDS => http_build_query($data)
));
// Send the request
$KReresponse = curl_exec($chss);
$KError = curl_error($chss);
// Decode the response
$KReresponseData = json_decode($KReresponse, TRUE);
echo '<br>';
echo("Response:");
print_r($KReresponseData);
echo '<br>';
echo("Error:");
print_r($KError);
}
?>
<form name="upload-form" method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data">
<input type="file" name="fileUploaded">
<input type="submit" name="submit" value="Upload">
</form>
Any help would be much appreciated!
Try this:
$filePath = "#{$_FILES['fileUploaded']['tmp_name']};filename={$_FILES['fileUploaded']['name']};type={$_FILES['fileUploaded']['type']}";
I'm trying to write a very simple test to upload an image to imgur and then redirect to the imgur page once it's done. (haven't gotten to that point yet)
I've looked all around these forums and tried so many different codes posted here, as well as cobbled some together from various posts. Finally, I've stopped getting errors in the php.
But.. in every code I've seen, it displays the image once it's done. Using that exact same code, my image does not appear, all I get is a page with Result: and nothing more. Does anyone have any idea what could be the problem?
<html>
<body>
<form action="upload_img.php" method="post" enctype="multipart/form-data">
<input type="file" name="imgupload" /><br>
<input type="submit" value="Upload to Imgur" />
</form>
</body>
</html>
and the php file:
<?php
$client_id = '$clientIDHERE';
$file = file_get_contents($_FILES["imgupload"]["tmp_name"]);
$url = 'https://api.imgur.com/3/image.json';
$headers = array("Authorization: Client-ID $client_id");
$pvars = array('image' => base64_encode($file));
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL=> $url,
CURLOPT_TIMEOUT => 30,
CURLOPT_POST => 1,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_POSTFIELDS => $pvars
));
$json_returned = curl_exec($curl); // blank response
echo "Result: " . $json_returned ;
curl_close ($curl);
?>
Before you close the $curl handle you should check for errors:
if ($error = curl_error($curl)) {
die('cURL error:'.$error);
}
I've got the following code and it works perfectly fine for uploading one image to Imgur using their API:
$client_id = $myClientId;
$file = file_get_contents($_FILES["file"]["tmp_name"]);
$url = 'https://api.imgur.com/3/image.json';
$headers = array("Authorization: Client-ID $client_id");
$pvars = array('image' => base64_encode($file));
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL=> $url,
CURLOPT_TIMEOUT => 30,
CURLOPT_POST => 1,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_POSTFIELDS => $pvars
));
$json_returned = curl_exec($curl); // blank response
$json = json_decode($json_returned, true);
curl_close ($curl);
However I need to upload multiple images at once. On the client side, the user will have multiple <input type="file" /> fields. I'm completely stuck now with figuring out where and how I will need to modify this code in order to handle multiple image upload when they come through to the server in the form of an array. Does anyone have any ideas?
Change the markup as follows:
<form action="file-upload.php" method="post" enctype="multipart/form-data">
Send these files:<br />
<input name="file[]" type="file" multiple="multiple" /><br />
<input type="submit" value="Send files" />
</form>
Now, you can loop through the $_FILES array using a foreach, like so:
foreach ($_FILES['file']['tmp_name'] as $index => $tmpName) {
if( !empty( $tmpName ) && is_uploaded_file( $tmpName ) )
{
// $tmpName is the file
// code for sending the image to imgur
}
}