I have already tried below code,
function upload_content(){
// Authorization info
$tumblr_email = 'email-address#host.com';
$tumblr_password = 'secret';
// Data for new record
$post_type = 'text';
$post_title = 'Host';
$post_body = 'This is the body of the host.';
// Prepare POST request
$request_data = http_build_query(
array(
'email' => $tumblr_email,
'password' => $tumblr_password,
'type' => $post_type,
'title' => $post_title,
'body' => $post_body,
'generator' => 'API example'
)
);
// Send the POST request (with cURL)
$c = curl_init('api.tumblr.com/v2/blog/gurjotsinghmaan.tumblr.com/post');
//api.tumblr.com/v2/blog/{base-hostname}/post
//http://www.tumblr.com/api/write
//http://api.tumblr.com/v2/blog/{base-hostname}/posts/text?api_key={}
curl_setopt($c, CURLOPT_POST, true);
curl_setopt($c, CURLOPT_POSTFIELDS, $request_data);
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($c);
$status = curl_getinfo($c, CURLINFO_HTTP_CODE);
curl_close($c);
// Check for success
if ($status == 201) {
echo "Success! The new post ID is $result.\n";
} else if ($status == 403) {
echo 'Bad email or password';
} else {
echo "Error: $result\n";
}
}
You need to use the proper Tumblr endpoint:
http://www.tumblr.com/api/write
I'm pretty sure the others won't work.
Obviously make sure that your user and pass are correct, other than that, this looks fine - it's pretty much exactly what I'd write.
Related
i have created a form in codeigniter and given a google captcha v2, i have created the site key and secret key added it to config files, and also added the js and the recaptcha div including my site key. the following is my captcha function in controller:
public function validate_captcha() {
$recaptcha = trim($this->input->post('g-recaptcha-response'));
$userIp= $this->input->ip_address();
$secret='xxxxxxxxxxxx'; (i have given my scret key here)
$secretdata = array(
'secret' => "$secret",
'response' => "$recaptcha",
'remoteip' =>"$userIp"
);
$verify = curl_init();
curl_setopt($verify, CURLOPT_URL, "https://www.google.com/recaptcha/api/siteverify");
curl_setopt($verify, CURLOPT_POST, true);
curl_setopt($verify, CURLOPT_POSTFIELDS, http_build_query($secretdata));
curl_setopt($verify, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($verify, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($verify);
$status= json_decode($response, true);
if(empty($status['success'])){
return FALSE;
}else{
return TRUE;
}
}
the following is my register form function in same controller:
public function ajaxRegAction() {
$this->load->library('session');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST, PATCH, PUT, DELETE, OPTIONS');
header('Access-Control-Allow-Headers: Origin, Content-Type, X-Auth-Token');
$this->load->library('form_validation');
$utype='W';
$dhamu = $this->validate_captcha();
$createddate = date('Y-m-d h:i:s');
$createdby = '0';
$mobile = $this->input->post('phone');
$form_data = array(
'type' => $utype,
'unique_id' => $this->mainModel->generateIndividualID(),
'phone_num' => $mobile,
'email' => $this->input->post('email'),
'first_name' => $this->input->post('firstname'),
'last_name' => $this->input->post('lastname'),
'created_by' => $createdby,
'created_date' => $createddate,
);
$name = $this->input->post('firstname')." ".$this->input->post('lastname');
$access_info = array('user_type'=>$utype);
$check = $this->mainModel->checkUserAvail($this->input->post('phone'),$this->input->post('email'));
$checkauser = $this->mainModel->checkAjaxUser($this->input->post('phone'),$this->input->post('email'));
if($check==0) {
if($dhamu==1){
$insert = $this->mainModel->ajaxRegInsertUser($form_data, $access_info,$this->input->post('password'));
$message="Dear ".$this->input->post('firstname').", You have successfully registered with Book The Party. Thank You for coming On-Board. Contact us at 9666888000 for any queries - Team BTP";
$email_message=$message;
$message=rawurlencode($message);
$this->mainModel->sendOtptoCustomer($mobile,$message);
$this->mainModel->sendmailtoCustomer($this->input->post('email'),$email_message);
echo "success";
}
else{ echo "Captcha Error";}
}
else{
echo "Registration Failed !";
}
even if i check the google recaptcha box and press register, the form is showing "Captcha Error", values are not being added to the database also. can anyone please tell me what could be wrong here, thanks in advance
Step 1:
Make sure you have added localhost in your domain in Google Captcha V2 dashboard.
Step 2:
I am modifying your function you can use it like this:
public function validate_captcha()
{
if(isset($_POST['g-recaptcha-response']))
{
$captcha=$_POST['g-recaptcha-response'];
}
$secretKey = "Put your secret key here";
$ip = $_SERVER['REMOTE_ADDR'];
// post request to server
$url = 'https://www.google.com/recaptcha/api/siteverify?secret=' . urlencode($secretKey) . '&response=' . urlencode($captcha);
$response = file_get_contents($url);
$responseKeys = json_decode($response,true);
// should return JSON with success as true
if($responseKeys["success"]) {
return TRUE;
} else {
return FALSE;
}
}
instead of CURL
Do let me know if this works
I'm trying to implement Google ReCaptcha V2 in a PHP form.
Here is my code :
$arrContextOptions=array(
"ssl"=>array(
"verify_peer"=>false,
"verify_peer_name"=>false,
),
);
if($_SERVER["REQUEST_METHOD"] === "POST")
{
//form submitted
//check if other form details are correct
//verify captcha
$recaptcha_secret = "";
$response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secret."&response=".$_POST['g-recaptcha-response'], false, stream_context_create($arrContextOptions));
$response = json_decode($response, true);
if($response["success"] === true)
{
echo "Logged In Successfully";
}
else
{
echo "You are a robot";
}
}
?>
When i submit my form, it always return
You are a robot
.
My public key is correct, and my private key too.
I don't know what i'm doing wrong ?
I'm working as localhost.
Thanks.
Just integrated 2 days ago the V2 recaptcha from Google
Try my code below, explicitly to see if is solving your problem:
I can see u do file_get_contents, and i think here is your issues, u have to make POST, please use my code below
if($_SERVER["REQUEST_METHOD"] === "POST"){
// prepare post variables
$post = [
'secret' => $secret,
'response' => $_POST['g-recaptcha-response'],
'remoteip' => 'is optional, but i pass it',
];
$ch = curl_init('https://www.google.com/recaptcha/api/siteverify');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$response = curl_exec($ch);
curl_close($ch);
var_dump($response);
$response = json_decode($response, true);
// check result
if(isset($response['success']) && $response['success'] == true){
echo "Logged In Successfully";
}else{
echo "You are a robot";
}
}
I need your help with this:
I try to create a shot in Dribbly but get the error message.
I did it like described in the documentation(http://developer.dribbble.com/v1/shots/#create-a-shot). This is my code:
$url = "http://www.rodos-palace.gr/uploads/images/509.jpg";
print_r($drib->create_shot('shottitle', $url));
public function create_shot($title, $image, $description = false)
{
$query = array(
'title' => $title,
'image' => $image
);
if ($description) {
$query['description'] = $description;
}
// print_r("<br>crearing_shot-> ".__LINE__);
$query['access_token'] = $this->access_token;
return $this->curl_post_shot($this->short_api, $query);
// return $this->curl_post_shot($this->short_api . "?" . 'access_token=' . $this->access_token, http_build_query($query));
}
public function curl_post_shot($url, $post)
{
$headers = array("Content-Type: multipart/form-data");
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $url,
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $post,
CURLOPT_HTTPHEADER => $headers,
/*, // stop after 10 redirects
CURLOPT_SSL_VERIFYPEER => false*/
));
curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER, true );
curl_setopt($curl, CURLINFO_HEADER_OUT, true);
$information = curl_getinfo($curl);
if( ($resp = curl_exec($curl)) === false )
{
echo 'Curl error: ' . curl_error($curl);
}
else
{
// echo 'Operation completed without any errors';
}
//echo "Check HTTP status code >>>>";
// Check HTTP status code
if (!curl_errno($curl)) {
switch ($information = curl_getinfo($curl, CURLINFO_HTTP_CODE)) {
case 200: # OK
break;
default:
{ /*echo 'Unexpected HTTP code: ', $http_code, "\n";*/}
}
}
curl_close($curl);
print_r("<br>crearing_shot-> ".__LINE__);
echo "<pre>";
var_dump($information);
print_r("<br>----------------------------<br>");
print_r($headers);
return $resp;
}
and this is the error:
{ "message": "Validation failed.", "errors": [
{
"attribute": "image",
"message": "file is required"
} ] }
beside this, I try to do it with the image real path and with the PHP $_FILES and the result is the same.
please help me to solve this problem.
Thanks a lot.
Since I cannot verify, because I do not have an uploader account, I recommend using the cURL file class to upload a file. There are multiple ways to to this, but since you use OOP you should try this:
public function create_shot($title, $image, $description = false)
{
$query = array(
'title' => $title,
'image' => new CURLFile($image), // $image is the absolute path to the image file
);
if ($description) {
$query['description'] = $description;
}
$query['access_token'] = $this->access_token;
return $this->curl_post_shot($this->short_api, $query);
}
You will find details in the manual about the file upload.
I'm using Quickblox to send push notifications to iPhone users. I created PHP functions for session and creating a user and API is working fine, but testing sending a push notification, I got an error on Dashboard/Queue: "incorrect event" on the message column. The event is created but never arrived.
The response of the API is OK, like the documentation.
I don't know why I got that error.
This is my code:
if (isset($_POST['mensaje'])) {
// Quickblox user Sign Up
$session = createSession( . . . , '...', '...', '...', '...');
$token = $session->token;
$group = '...'; // Hardcoded only for testing
$resp = sendQBPush($_POST['mensaje'], $group, $token);
}
and the function:
function sendQBPush($msg, $group, $token)
{
if (!$msg) {
return false;
}
$message = base64_encode($msg);
// Build post body
$post_body = array(
'event' => array(
'notification_type' => 'push',
'environment' => 'production',
'user' => array(
'tags' => array(
'any' => $group
)
) ,
'push_type' => 'apns',
'message' => 'payload=' . $message
)
);
$request = json_encode($post_body);
$ch = curl_init('http://api.quickblox.com/events.json');
curl_setopt($ch, CURLOPT_POST, true); // Use POST
curl_setopt($ch, CURLOPT_POSTFIELDS, $request); // Setup post body
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'QuickBlox-REST-API-Version: 0.1.0',
'QB-Token: ' . $token
));
$resultJSON = curl_exec($ch);
$responseJSON = json_decode($resultJSON);
echo $resultJSON;
// Check errors
if ($responseJSON) {
return $responseJSON;
}
else {
$error = curl_error($curl) . '(' . curl_errno($curl) . ')';
return $error;
}
// Close connection
curl_close($curl);
}
Thanks for your help
Could anyone help me figure out how to post to tumblr using php.
I tried googling for a library or a sample code but couldn't find one. all I can find is this here https://github.com/alexdunae/tumblr-php/blob/master/Tumblr.php and it doesnt seem to work also I looked and tried the code on v1 api at tumblr website that doesnt work either ....
function post($data){
if(function_exists("curl_version")){
$data["email"] = $this->email;
$data["password"] = $this->password;
$data["generator"] = $this->generator;
$request = http_build_query($data);
$c = curl_init('http://www.tumblr.com/api/write');
curl_setopt($c,CURLOPT_POST,true);
curl_setopt($c,CURLOPT_POSTFIELDS,$request);
curl_setopt($c,CURLOPT_RETURNTRANSFER,true);
$return = curl_exec($c);
$status = curl_getinfo($c,CURLINFO_HTTP_CODE);
curl_close($c);
if($status == "201"){
return true;
}
elseif($status == "403"){
return false;
}
else{
return "error: $return";
}
}
else{
return "error: cURL not installed";
}
}
Thanks for the help
I just noticed that this is showing up as Featured for Tumblr and I want to say this: As of 2012, you should IGNORE the above answer by Tuga because it DOES NOT work with the newest Tumblr API.
What you need is TumblrOAuth which is built from OAuth Sandbox.
It is only setup to read and write Tumblr posts, so if you want to do more than that, you'll need to alter the code. I used it as my code base for Followr.
Stolen from http://www.tumblr.com/docs/en/api
// Authorization info
$tumblr_email = 'info#davidville.com';
$tumblr_password = 'secret';
// Data for new record
$post_type = 'regular';
$post_title = 'The post title';
$post_body = 'This is the body of the post.';
// Prepare POST request
$request_data = http_build_query(
array(
'email' => $tumblr_email,
'password' => $tumblr_password,
'type' => $post_type,
'title' => $post_title,
'body' => $post_body,
'generator' => 'API example'
)
);
// Send the POST request (with cURL)
$c = curl_init('http://www.tumblr.com/api/write');
curl_setopt($c, CURLOPT_POST, true);
curl_setopt($c, CURLOPT_POSTFIELDS, $request_data);
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($c);
$status = curl_getinfo($c, CURLINFO_HTTP_CODE);
curl_close($c);
// Check for success
if ($status == 201) {
echo "Success! The new post ID is $result.\n";
} else if ($status == 403) {
echo 'Bad email or password';
} else {
echo "Error: $result\n";
}
?>
$conskey = "CONSUMER KEY";
$conssec = "CONSUMER SECRET";
$tumblr_blog = "myblog.tumblr.com";
$to_be_posted = "This is the text to be posted";
$oauth = new OAuth($conskey,$conssec);
$oauth->fetch("http://api.tumblr.com/v2/blog/".$tumblr_blog."/post", array('type'=>'text', 'body'=>$to_be_posted), OAUTH_HTTP_METHOD_POST);
$result = json_decode($oauth->getLastResponse());
if($result->meta->status == 200){
echo 'Success!';
}
This code will let you post to your tumblr blog using tumblr API.
I hope this code helps.
The api example provided by Tuga is working for me (on Wordpress)...so I think your problem lies elsewhere, and not with the example provided. I would also be very appreciative if you guys got a version 2 api working if you could post it.