So I am currently working on a bunch of curl statements in PHP with a sql table to modify or create new keys, based on the $flag. This will be of set size 2 without change (or atleast for now).
What I am wondering is if Flag1 exists in this url, I would want to use a PUT, but Flag2 does not exist, I would need a POST. How would I be able to modify this code to determine PUT vs POST.
The loop logic has not been implemented yet. For now I am just trying to put $flag[0] in with $new_Val[0] and trying to modify PUT vs POST but I dont know how.
$flag = array("Flag1", "Flag2");
$new_Val = array("Val1", "Val2");
$url = "myurl/thing/$flag/id/1112";
$my_data = array( "Key" => "$flag[0]", "Value" => "$new_Val[0]");
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($my_data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array( stuff in here)
result = curl_exec($ch);
$json = json_decode($result);
Related
I'm new to the Mailchimp api v3.0 (using php). I've created a campaign with the api and want to retrieve the campaign id from the cURL return data but can't seem to extract just the id. I've been away from php for awhile so I guess I'm just being brain dead. Clearly, I can print the entire result but just need to extract the campaign id in order to add/update content. Most of the test code follows. How do I extract just the id from $retval?
$json_data = json_encode($options);
$auth = base64_encode('prcAdmin:'. $apikey);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://usn.api.mailchimp.com /3.0/campaigns');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content_Type: application/json',
'Authorization: Basic ' . $auth));
curl_setopt($ch, CURLOPT_USERAGENT, 'PHP-MCAPI/3.0');
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
$retval = curl_exec($ch);
A few typos in your code, let's fix them first.
An extra space in the url
Content_Type to Content-Type. Good to correct it, though the endpoint returns data in a JSON format.
Assuming that you have a valid Auth with valid campaign data, it should return campaign data in JSON format. To extract the campaign id is pretty easy.
$campaign = json_decode($retval, true);
$campaignId = $campaign['id'];
If you can't get correct a campaign id, try to print out the result by using print_r($retval);. The correct data should look similar to an example response found on this page - http://developer.mailchimp.com/documentation/mailchimp/reference/campaigns/. (From and between curly brackets).
I resolved the problem: I added a:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
and it obviously returned the response differently so the "id" tag could be referenced as:
$id = json_decode($retval);
echo $id->id;
Now it extracts just the "id".
I am using a REST API to interface a Joomla form to Salesforce.
I have successfully authenticated, and made queries, and updated existing records.
When trying to add a record however there is no result.
The code I have is this:
<?php
//
// test add of a Contact
//
$data3 = array ( 'FirstName' => 'John',
'LastName' => 'Testing',
'Name' => 'John Testing' );
$data3_param = json_encode ($data3, true);
$form->data['data3'] = $data3;
$bearer = "Authorization : Bearer ".$form->data['access'];
$hdrfields = array();
$hdrfields[] = $bearer;
$hdrfields[] = "Content-Type: application/json";
$url = "https://ap4.salesforce.com/services/data/v".$form->data['latestversion'];
$e = "/sobjects/Contact/";
$url2 = $url.$e;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url2);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_HTTPHEADER, $hdrfields);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $data3_param);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response3 = curl_exec($ch);
curl_close($ch);
$form->data['response3'] = $response3;
?>
So this code is similar to the record update except that there is no record Id in the URL. As far as I can see it follows the documentation, but obviously there is something wrong. The three fields are the mandatory fields in the table where I want to add the record.
I hope someone can see what I have done wrong.
Thank you
Tim
Problem solved - user error. My apologies.
Seams you just forgot required field - AccountId. In salesforce each contact linked to some account. BTW, 'Name' field is not required, it's just calculated in SF as concatenation of first and last names.
I am new in PayPal API and I am using ExpressCheckout for all payment transactions. What I want is, To get all the payments received transactions using start_time and end_time so that I will know what are the status of this transactions.
The purpose of this is that my system is aware about the status of all payments from time to time.
I am also confused if this is possible in sandbox. If it is possible please help me.
I am working this for a week using php language. I follow the link in PayPal but I still don't get the it. https://developer.paypal.com/docs/api/#paging--filtering
Please if you have a nice tip please help me to solve my problem.
#Developer Status' answer is a good sample, but I would recommend using this PayPal PHP SDK, specifically the TransactionSearch template which makes the call very simple for you. It handles parsing all of the results for you, too. Here you can see a sample of the full result including parsed search results (you may need to scroll down a little to see the parsed SEARCHRESULTS.)
As you loop through those results you'll most likely need to also hit GetTransactionDetails for each one to obtain all of the info you need. Again, that template in the SDK will make that very simple for you.
So if you download that SDK, adjust the config file with your own API credentials, and then load up that sample/template you can have this working within minutes.
I would also recommend that you take a look at PayPal IPN. This will allow you to get real-time updates when transactions hit your account so you can automate everything in real-time as opposed to hitting the TransactionSearch API at specific intervals.
# You can put start date and end date here in request for `STARTDATE` AND `ENDDATE` #
<?php
$info = 'USER=[API_USERNAME]'
.'&PWD=[API_PASSWORD]'
.'&SIGNATURE=[API_SIGNATURE]'
.'&METHOD=TransactionSearch'
.'&TRANSACTIONCLASS=RECEIVED'
.'&STARTDATE=2013-01-08T05:38:48Z'
.'&ENDDATE=2013-07-14T05:38:48Z'
.'&VERSION=94';
$curl = curl_init('https://api-3t.paypal.com/nvp');
curl_setopt($curl, CURLOPT_FAILONERROR, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_POSTFIELDS, $info);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_POST, 1);
$result = curl_exec($curl);
# Bust the string up into an array by the ampersand (&)
# You could also use parse_str(), but it would most likely limit out
$result = explode("&", $result);
# Loop through the new array and further bust up each element by the equal sign (=)
# and then create a new array with the left side of the equal sign as the key and the right side of the equal sign as the value
foreach($result as $value){
$value = explode("=", $value);
$temp[$value[0]] = $value[1];
}
# At the time of writing this code, there were 11 different types of responses that were returned for each record
# There may only be 10 records returned, but there will be 110 keys in our array which contain all the different pieces of information for each record
# Now create a 2 dimensional array with all the information for each record together
for($i=0; $i<count($temp)/11; $i++){
$returned_array[$i] = array(
"timestamp" = urldecode($result["L_TIMESTAMP".$i]),
"timezone" = urldecode($result["L_TIMEZONE".$i]),
"type" = urldecode($result["L_TYPE".$i]),
"email" = urldecode($result["L_EMAIL".$i]),
"name" = urldecode($result["L_NAME".$i]),
"transaction_id" = urldecode($result["L_TRANSACTIONID".$i]),
"status" = urldecode($result["L_STATUS".$i]),
"amt" = urldecode($result["L_AMT".$i]),
"currency_code" = urldecode($result["L_CURRENCYCODE".$i]),
"fee_amount" = urldecode($result["L_FEEAMT".$i]),
"net_amount" = urldecode($result["L_NETAMT".$i]));
}
?>
Try This
<?
$info = 'USER=[API_USERNAME]'
.'&PWD=[API_PASSWORD]'
.'&SIGNATURE=[API_SIGNATURE]'
.'&METHOD=TransactionSearch'
.'&TRANSACTIONCLASS=RECEIVED'
.'&STARTDATE=2013-01-08T05:38:48Z'
.'&ENDDATE=2013-07-14T05:38:48Z'
.'&VERSION=94';
$curl = curl_init('https://api-3t.paypal.com/nvp');
curl_setopt($curl, CURLOPT_FAILONERROR, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_POSTFIELDS, $info);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_POST, 1);
$result = curl_exec($curl);
$result = explode("&", $result);
foreach($result as $value){
$value = explode("=", $value);
$temp[$value[0]] = $value[1];
}
foreach($temp as $k=>$v){
$i++;
preg_match('#^(.*?)([0-9]+)$#is',$k,$str);
$num=$str[2];
$key=preg_replace('#^[A-z]_#','',$str[1]);
if($key!=''){
$new[$num][$key]=urldecode($v);
}
}
print_R($new);
?>
This example only displayed a blank page for me.
This one did as well.
I've got the latest version of PHP and cURL set up properly, as far as I know so there shouldn't be any problem at that end. I'd prefer JavaScript to retrieve products but I'm open minded.
I happen to not be highly skilled, but I'd like to get my foot in the door.
edit: I will show you the code that doesn't work, and the error it is giving me.
<?php
// Your developer key
$cj_id = "My ID - omitted for privacy.";
// Your website ID
$website_id = "Also removed for privacy.";
// Keywords to search for
$keywords = "credit+card";
// URL to query with cURL
$url = "https://product-search.api.cj.com/v2/product-search?website-id=$website_id&keywords=$keywords";
// Initiate the cURL fetch
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
// Send authorization header with the CJ ID. Without this, the query won't work
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: '.$cj_id));
$result = curl_exec($ch);
// Put the results to an object
$resultXML = simplexml_load_string($result);
// Print the results
print "<pre>";
print_r($resultXML);
print "</pre>";
?>
Now, this is the error that it's giving me.
SimpleXMLElement Object
(
[error-message] => Invalid Key provided. Valid keys are: advertiser-ids, advertiser-sku, currency, high-price, high-sale-price, isbn, keywords, low-price, low-sale-price, manufacturer-name, manufacturer-sku, page-number, records-per-page, serviceable-area, sort-by, sort-order, upc, website-id
)
You have a error in your URL, try this:
$url = "https://product-search.api.cj.com/v2/product-search?website-id=$website_id&keywords=$keywords";
instead of :
$url = "https://product-search.api.cj.com/v2/product-search?website-id=$website_id&keywords=$keywords";
<?php
echo '<pre>';
$url='https://product-search.api.cj.com/v2/product-search?website-id=your-id-key-here&advertiser-ids=4415206&records-per-page=999&serviceable-area=US';
$CJ_KEY='0085eb59c8928f028ba5b27bccfe17cdd20cf4e9079b977b2cc6df72752abab9205676a2f7ee67befe9dccab85f656ef46aba49e500faccbf75dfc6e03f655334d/00848a3f9bf0e13525bce27f008d6245c3e42ae80f2d80a8d9d2220807ca386f4b10146cbbcfff06aafb5e49c03a3318213389dee7861abb2dd7229470390a89c9';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, FAlSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: '.$CJ_KEY));
$curl_results = curl_exec($ch);
$xml = simplexml_load_string($curl_results);
var_dump($xml);
// Loop Insert Product to database
echo '<pre>';
// if you no set: records-per-page=999, default get 50 products latest
// advertiser-ids=4415206 is Id of Advertiser in CJ, you can replace other id ,
Hope helpful for you , good luck !
?>
Here's the setup:
I have a site setup for a client. The customer:
Visits the site
Enters in basic information for our records
Proceeds to PayPal via a "Buy Now" button
Makes the payment through PayPal
Returns to the site
What I am wanting to know is how do I get a list of all the transactions? I have the PayPal login as well as the API username, password, and signature, but for the life of me I cannot find a single place on the internet that gives an example of how to pull a list of transactions from PayPal either via PHP or jQuery/Javascript/Ajax.
Does anyone have any ideas? examples?
Thanks in advance.
UPDATE:
I was able to develop a solution to this question. See my answer below with code and comments.
Ok, so I finally was able to develop something that works. The code is posted below with a link to the TransactionSearch API options from PayPal
https://developer.paypal.com/webapps/developer/docs/classic/api/merchant/TransactionSearch_API_Operation_NVP/
<?php
$info = 'USER=[API_USERNAME]'
.'&PWD=[API_PASSWORD]'
.'&SIGNATURE=[API_SIGNATURE]'
.'&METHOD=TransactionSearch'
.'&TRANSACTIONCLASS=RECEIVED'
.'&STARTDATE=2013-01-08T05:38:48Z'
.'&ENDDATE=2013-07-14T05:38:48Z'
.'&VERSION=94';
$curl = curl_init('https://api-3t.paypal.com/nvp');
curl_setopt($curl, CURLOPT_FAILONERROR, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_POSTFIELDS, $info);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_POST, 1);
$result = curl_exec($curl);
# Bust the string up into an array by the ampersand (&)
# You could also use parse_str(), but it would most likely limit out
$result = explode("&", $result);
# Loop through the new array and further bust up each element by the equal sign (=)
# and then create a new array with the left side of the equal sign as the key and the right side of the equal sign as the value
foreach($result as $value){
$value = explode("=", $value);
$temp[$value[0]] = $value[1];
}
# At the time of writing this code, there were 11 different types of responses that were returned for each record
# There may only be 10 records returned, but there will be 110 keys in our array which contain all the different pieces of information for each record
# Now create a 2 dimensional array with all the information for each record together
for($i=0; $i<count($temp)/11; $i++){
$returned_array[$i] = array(
"timestamp" => urldecode($temp["L_TIMESTAMP".$i]),
"timezone" => urldecode($temp["L_TIMEZONE".$i]),
"type" => urldecode($temp["L_TYPE".$i]),
"email" => urldecode($temp["L_EMAIL".$i]),
"name" => urldecode($temp["L_NAME".$i]),
"transaction_id" => urldecode($temp["L_TRANSACTIONID".$i]),
"status" => urldecode($temp["L_STATUS".$i]),
"amt" => urldecode($temp["L_AMT".$i]),
"currency_code" => urldecode($temp["L_CURRENCYCODE".$i]),
"fee_amount" => urldecode($temp["L_FEEAMT".$i]),
"net_amount" => urldecode($temp["L_NETAMT".$i]));
}
?>
Also, I came up with this nifty little, simple script to get more details about a particular transaction:
https://developer.paypal.com/webapps/developer/docs/classic/api/merchant/GetTransactionDetails_API_Operation_NVP/
<?php
$info = 'USER=[API_USERNAME]'
.'&PWD=[API_PASSWORD]'
.'&SIGNATURE=[API_SIGNATURE]'
.'&VERSION=94'
.'&METHOD=GetTransactionDetails'
.'&TRANSACTIONID=[TRANSACTION_ID]'
.'&STARTDATE=2013-07-08T05:38:48Z'
.'&ENDDATE=2013-07-12T05:38:48Z';
$curl = curl_init('https://api-3t.paypal.com/nvp');
curl_setopt($curl, CURLOPT_FAILONERROR, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_POSTFIELDS, $info);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_POST, 1);
$result = curl_exec($curl);
parse_str($result, $result);
foreach($result as $key => $value){
echo $key.' => '.$value."<BR>";
}
?>
They have a TransactionSearch API:
https://developer.paypal.com/webapps/developer/docs/classic/api/merchant/TransactionSearch_API_Operation_NVP/
I use mine pull by invoice number to retrieve transaction ids for refunding.
<script
data-env="sandbox"
data-tax="0.00"
data-shipping="0.00"
data-currency="USD"
data-amount="0.00"
data-quantity="0"
data-name="No Item Selected"
data-button="buynow" src="https://www.paypalobjects.com/js/external/paypal-button.min.js?merchant=ben4xfer#gmail.com" async="async"></script>
This is a html element that hooks up to the paypal API to make a transaction when the button is clicked. Do not change the data-button, src, or async properties. Completly remove the data-env node after you are finished testing (the data-env node prevents an actual charge from being made while you are testing). Change all of the other properties according to their names (e.g you would change data-name to the name of the product you are selling). Insert the element as you would any other html element (e.g. <p>).