Gravity Forms Web API Update Entry - php

I'm trying to use the Gravity Forms Web API to update an entry, this is the php code I have at the moment.
<?php
$api_key = '';
$private_key = '';
$method = 'PUT';
$endpoint = 'https://www.website.co.uk/gravityformsapi/';
//$route = 'entries';
$route = 'entries/61';
$expires = strtotime('+60 mins');
$string_to_sign = sprintf('%s:%s:%s:%s', $api_key, $method, $route, $expires);
$sig = calculate_signature($string_to_sign, $private_key);
$api_call = $endpoint.$route.'?api_key='.$api_key.'&signature='.$sig.'&expires='.$expires;
//array of entries (each entry is an array with key=field id)
$entries = array(
array("status"=>"active","1.3"=>$_POST['first_name'],)
);
$ch = curl_init($api_call);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($entries));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
$result = curl_exec($ch);
function calculate_signature($string, $private_key) {
$hash = hash_hmac("sha1", $string, $private_key, true);
$sig = rawurlencode(base64_encode($hash));
return $sig;
};
?>
The response I'm getting is that is successfully updated the post, but instead of updating the post, it seems to delete the post. Can anyone see anything wrong with my code? If I refresh the page of the entry I edited on Wordpress I get this error.
Fatal error: Cannot use object of type WP_Error as array in /data02/c6536622/public_html/wp-content/plugins/gravityforms/entry_detail.php on line 57
Is there anyway to get a log to find out what's going wrong using Gravity Forms, or WordPress?

As you're updating a single entry just send the one entry instead of the collection. Also, send the whole entry or you'll end up deleting all the values for the missing fields.

Related

Zoho Get an Invoice

I am trying to integrate zoho with my web app. I am making an api call to get a particular invoice. The call as per documentation is
'https://books.zoho.com/api/v3/invoices/INV_ID?organization_id=XXXXXXXX'
This works fine as well. But when I set a variable for INVOICE ID, it returns all the invoices. (200 INVOICES) The call is below.
'https://books.zoho.com/api/v3/invoices/' .$inv_id. '?organization_id=XXXXXXXX';
where I am making the mistake? The method is below.
$zid = '';
if(isset($_POST['_zid'])){
$zid = $_POST['_zid'];
}
get_invoice($access_token, $zid);
function get_invoice($access_token, $zid){
$invid = $zid;
$service_url = $GLOBALS['service_url'] . 'https://books.zoho.com/api/v3/invoices/' .$invid. '?organization_id=XXXXXXXXX';
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_URL, $service_url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Authorization: Zoho-oauthtoken '. $access_token,
'Content-Type: "application/json"'));
$res = curl_exec($ch);
curl_close($ch);
$dec_res = array();
$dec_res = json_decode($res, true);
print_r($dec_res);
}
Everything is fine except a small thing. I didn't think of Array Declaration. The working call is straightly given an integer as the invoice_id. when I pass the variable declared as a string. so although the digits it is counted as a string which makes the call invalid.
//THIS WILL DO THE TRICK
$zid = 0;
if(isset($_POST['_zid'])){
$zid = $_POST['_zid'];
}

PHP - Sending data to Salesforce with cURL but not posting in database

my problem is that i'm working with salesforce in wordpress, i'm not using wordpress-to-lead plugin, I have a form in a template and that form sends data to salesforce via cURL and also is posting data in database cause I have to generate a password and then send it to the user but its not working, is working salesforce but not saving data in the database, here is my code to post data in database and generate the password
$keysString = implode(", ", array_keys($blank_section));
unset($_POST['userId']);
$user_id = mysql_query("SELECT MAX(id) AS id FROM int_form_data");
$user_id = $user_id+11;
$passwordSend = 'INTELIGOS'.rand(10000, 5000000);
$array_user_id = array('user_id' => $user_id, 'password' => $passwordSend);
$posted_data = array_merge($_POST,$array_user_id);
foreach($posted_data as $k=>$v) {
$itfdatainfo[$k] = $v;
}
$itfkeys = array_keys($itfdatainfo);
$itfvalues = array_values($itfdatainfo);
if(isset($_POST['submit'])) {
$sql = "INSERT INTO int_form_data (".implode(',',$itfkeys).") VALUES('".implode("','",$itfvalues)."')";
$result = mysql_query($sql);
}
And here I use cURL to send data to Salesforce:
//Initialize the $query_string variable for later use
$query_string = "";
$kv = array();
//If there are POST variables
if ($_POST) {
//Initialize the $kv array for later use
//For each POST variable as $name_of_input_field => $value_of_input_field
foreach ($_POST as $llav => $value) {
//Set array element for each POST variable (ie. first_name=Arsham)
$kv[] = stripslashes($llav)."=".stripslashes($value);
}
//Create a query string with join function separted by &
$query_string = join("&", $kv);
}
//Check to see if cURL is installed ...
if (!function_exists('curl_init')){
die('Sorry cURL is not installed!');
}
//The original form action URL from Step 2
$url = 'https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8';
//Open cURL connection
$ch = curl_init();
//Set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, count($kv));
curl_setopt($ch, CURLOPT_POSTFIELDS, $query_string);
//Set some settings that make it all work
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, FALSE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//Execute SalesForce web to lead PHP cURL
$result = curl_exec($ch);
//close cURL connection
curl_close($ch);
Anyone knows why is happening that? I have all the code in one template in wordpress
I'm looking at this INSERT INTO int_form_data (".implode(',',$itfkeys).") and thinking if some column name in $itfkeys needs to be enclosed in backticks then it would cause an sql error.

Real time page updates from Facebook

I have a web application which needs to fetch the real time updates to a particular page. I have gone through a lot of questions on this forums and yet not found anything that works for me. When I subscribe to the page updates
I am supplying valid app_id, app_secret and app_url.
<?php
$app_id = '';
$app_secret = '';
$app_url = '';
$fields = 'feed';
$verify_token = 'abcd#123';
// Fetching an App Token
$app_token_url = 'https://graph.facebook.com/oauth/access_token?client_id='
.$app_id.'&client_secret='.$app_secret
.'&grant_type=client_credentials';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $app_token_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$res = curl_exec($ch);
parse_str($res, $token);
if (isset($token['access_token'])) {
// Let's register a callback
$params = array(
'object'
=> 'page',
'fields'
=> $fields,
'callback_url'
// This is the endpoint that will be called when
// a User updates the location field
=> $app_url . '/index.php?action=callback',
'verify_token'
=> $verify_token,
);
curl_setopt($ch, CURLOPT_URL, 'https://graph.facebook.com/'
.$app_id.'/subscriptions?access_token='
.$token['access_token']);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
$res = curl_exec($ch);
if ($res && $res != 'null') {
print_r($res);
}
// Fetch list of all callbacks
curl_setopt($ch, CURLOPT_POST, 0);
$res = curl_exec($ch);
}
if ($res && $res != 'null') {
print_r($res);
error_log('updates = ' . print_r($res, true));
}
curl_close($ch);
?>
the FB posts to my callback URL but doesn't send the id of the post which was updated, instead it sends the user id in both the id as well as the uid. Even the updates are irregular, sometimes there is a notification, at other times no notification.
What do I need to do make this work?
There is something about whitelisting apps -- does that need to be done.
Do I need to make this app as a facebook app and have the page install this app on a tab?
Do I need special permission to be granted by the page admin.
Can this be done at all?
Any help would be very welcome. Thanks!!
A similar question on this forum :Facebook Real Time Update return only "changed_fields":["feed"] and not the actual comment

Python: using HMAC signing to use API (implementation from PHP example)

I have little trouble when i try to use bitcoin exchange API via python.
I have example function in PHP:
function bitmarket_api($method, $params = array())
{
$key = "my_key";
$secret = "my_secret";
$params["method"] = $method;
$params["tonce"] = time();
$post = http_build_query($params, "", "&");
$sign = hash_hmac("sha512", $post, $secret);
$headers = array(
"API-Key: " . $key,
"API-Hash: " . $sign,
);
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_URL, "https://www.bitmarket.pl/api2/");
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$ret = curl_exec($curl);
return json_decode($ret);
}
And it is my python implementation:
def bitmarket_api(method, params):
key = 'my_key'
secret = 'my_secret'
url = 'https://www.bitmarket.pl/api2/'
params['method'] = method
params['tonce'] = timestamp()
post = urllib.urlencode(params)
sign = base64.b64encode(str(HMAC(secret, post, sha512).digest()))
headers = {}
headers['API-Key:'] = key
headers['API-Hash:'] = sign
req = urllib2.Request(url, post, headers)
res = urllib2.urlopen(req, post)
return json.load(res)
So, when i try to to invoke info method (or other method) i get 'Invalid API key' error.
I was looking for solution, and i was trying several other approach with no succes.
Can u guys help me? I think problem can be in headers...
Please excuse my poor English. I try to do my best, but I'm still making mistakes.
Use the Python HMAC Auth lib with Requests lib
https://github.com/bazaarvoice/python-hmac-auth
Nice and tidy to let your build your client without too much trouble.

oAuth "Invalid Signature" When Adding a Flickr API Argument

I'm having trouble with the all-too-common oAuth "invalid signature" issue.
My language is PHP the API I'm trying to interact with is Flickr.
My goal is to call the flickr.contacts.getList method. I'm able to call this method without any problems, as long as I don't pass any arguments with my API call. As soon as I add in an argument (e.g., page), my oAuth signature gets invalidated.
For the most part, I'm leveraging someone else's code to do just about everything (see this blog post). As a result, I don't fully understand how the oAuth signature is getting built and how it is also getting invalidated. This is where I need some help.
The code directly below WORKS just fine. Note that I am NOT passing the page argument.
$mt = microtime();
$rand = mt_rand();
$oauth_nonce = md5($mt . $rand);
$nonce = $oauth_nonce;
$sig_method = "HMAC-SHA1";
$timestamp = gmdate('U');
$oversion = "1.0";
$request_token_url = 'http://api.flickr.com/services/rest';
$basestring = "format=json&method=flickr.contacts.getList&nojsoncallback=1&oauth_consumer_key=".$consumer_key."&oauth_nonce=".$nonce."&oauth_signature_method=".$sig_method."&oauth_timestamp=".$timestamp."&oauth_token=".$oauth_key."&oauth_version=".$oversion;
$baseurl = "GET&".urlencode($request_token_url)."&".urlencode($basestring);
$hashkey = $consumer_secret."&".$oauth_secret;
$oauth_signature = base64_encode(hash_hmac('sha1', $baseurl, $hashkey, true));
$fields = array
(
'method'=>'flickr.contacts.getList',
'oauth_nonce'=>$nonce,
'oauth_timestamp'=>$timestamp,
'oauth_consumer_key'=>$consumer_key,
'oauth_signature_method'=>$sig_method,
'oauth_version'=>$oversion,
'oauth_signature'=>$oauth_signature,
'nojsoncallback'=>'1',
'format'=>'json',
);
$fields_string = "";
foreach($fields as $key=>$value)
{
$fields_string .= "$key=".urlencode($value)."&";
}
$fields_string = rtrim($fields_string,'&');
$url = $request_token_url."?".$fields_string;
#Make Flickr API call.
$ch = curl_init();
$timeout = 5; // set to zero for no timeout
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$file_contents = curl_exec($ch);
curl_close($ch);
The code directly below DOES NOT WORK, as I get an "invalid signature" response from Flickr. Note that this time I am passing the page argument.
$mt = microtime();
$rand = mt_rand();
$oauth_nonce = md5($mt . $rand);
$nonce = $oauth_nonce;
$sig_method = "HMAC-SHA1";
$timestamp = gmdate('U');
$oversion = "1.0";
$request_token_url = 'http://api.flickr.com/services/rest';
$basestring = "format=json&method=flickr.contacts.getList&page=1&nojsoncallback=1&oauth_consumer_key=".$consumer_key."&oauth_nonce=".$nonce."&oauth_signature_method=".$sig_method."&oauth_timestamp=".$timestamp."&oauth_token=".$oauth_key."&oauth_version=".$oversion;
$baseurl = "GET&".urlencode($request_token_url)."&".urlencode($basestring);
$hashkey = $consumer_secret."&".$oauth_secret;
$oauth_signature = base64_encode(hash_hmac('sha1', $baseurl, $hashkey, true));
$fields = array
(
'method'=>'flickr.contacts.getList',
'oauth_nonce'=>$nonce,
'page'=>'1',
'oauth_timestamp'=>$timestamp,
'oauth_consumer_key'=>$consumer_key,
'oauth_signature_method'=>$sig_method,
'oauth_version'=>$oversion,
'oauth_token'=>$oauth_key,
'oauth_signature'=>$oauth_signature,
'nojsoncallback'=>'1',
'format'=>'json',
);
$fields_string = "";
foreach($fields as $key=>$value)
{
$fields_string .= "$key=".urlencode($value)."&";
}
$fields_string = rtrim($fields_string,'&');
$url = $request_token_url."?".$fields_string;
#Make Flickr API call.
$ch = curl_init();
$timeout = 5; // set to zero for no timeout
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$file_contents = curl_exec($ch);
curl_close($ch);
The only difference between the first and the second code sample is that I have added in an argument. I've done lots of testing, and this issue has nothing to do with the order in which the argument is passed (order doesn't seem to affect the signature). In addition, I've tried other Flickr API methods, and they all exhibit the same behavior (so this issue is not specific to this particular Flickr method).
I figured it out. Order does matter.
In the example above, to add on the page argument, you must add it on at the end of the $basestring variable, like so:
$basestring = "format=json&method=flickr.contacts.getList&page=1&nojsoncallback=1&oauth_consumer_key=".$consumer_key."&oauth_nonce=".$nonce."&oauth_signature_method=".$sig_method."&oauth_timestamp=".$timestamp."&oauth_token=".$oauth_key."&oauth_version=".$oversion."$page=1";
Order does not seem to matter when defining the $fields array.

Categories