Hey I'm trying to set up a remote cart. Very frustrating though since Amazon doesn't list any code examples for a remote cart without the customer leaving the site.
Here's where I'm at so far. I can get someone to leave my site and add the product from my site to Amazon using this (From: http://docs.aws.amazon.com/AWSECommerceService/latest/DG/AddToCartForm.html):
<form method="GET" action="http://www.amazon.com/gp/aws/cart/add.html">
<input type="hidden" name="AWSAccessKeyId" value="Access Key ID" /> <br/>
<input type="hidden" name="AssociateTag" value="Associate Tag" /><br/>
<p>One Product<br/>
ASIN:<input type="text" name="ASIN.1"/><br/>
Quantity:<input type="text" name="Quantity.1"/><br/>
<p>Another Product<br/>
ASIN:<input type="text" name="ASIN.2"/><br/>
Quantity:<input type="text" name="Quantity.2"/><br/>
</p>
<input type="submit" name="add" value="add" />
</form>
But I want to make it so they can add an item to cart and stay on my site. It seems like this is how I accomplish that:
http://webservices.amazon.com/onca/xml?
Service=AWSECommerceService&
AWSAccessKeyId=[AWS Access Key ID]&
AssociateTag=[Associate Tag]&
Operation=CartCreate&
Item.1.OfferListingId=B000062TU1&
Item.1.Quantity=2
&Timestamp=[YYYY-MM-DDThh:mm:ssZ]
&Signature=[Request Signature]
But when I'm totally confused as to how to generate a timestamp and signature. Do I put this into a form action? Is there anywhere with code examples? I've been searching all day and can't find it. Any help greatly appreciated.
I'm using this method to generate api request url, and its totally working fine. Believe this will help you
$uri = "/onca/xml";
$asin = "B00C5AHTC0";
$end_point = "webservices.amazon.com";
$params = array(
"Service" => "AWSECommerceService",
"Operation" => "CartCreate",
'Version' => "2013-08-01",
"AWSAccessKeyId" => AWS_ACCESS_KEY,
"AssociateTag" => AWS_ASSOCIATE_TAG,
"Item.1.ASIN" => $asin,
"Item.1.Quantity" => "5",
"Timestamp"=> gmdate('Y-m-d\TH:i:s\Z')
);
// Sort the parameters by key
ksort($params);
$pairs = array();
foreach ($params as $key => $value) {
array_push($pairs, rawurlencode($key) . "=" . rawurlencode($value));
}
// Generate the canonical query
$canonical_query_string = join("&", $pairs);
$string_to_sign = "GET\n" . $end_point. "\n" . $uri . "\n" . $canonical_query_string;
$signature = base64_encode(hash_hmac("sha256", $string_to_sign, AWS_SECRET_KEY, true));
$request_url = 'http://' . $end_point. $uri . '?' .$canonical_query_string . '&Signature='.rawurlencode($signature);
Related
I'm on a private php heroku repo, and want to create a form that can allow users on my website to input their email address and submit the form and add the email to an existing mailing list on mailgun.
I've contacted MailGun support, they've said its possible and to look at the documentation.
The docs are vague, I can't figure it out on my own.
Is there code examples you can give me that may point me in the right direction?
This is the line of thought:
* Step 1 - Create a mailinglist that will contain your list of emails.
* Step 2 - Create a form that asks a user for his email address, asks him to confirm the use of his email, sends an email address to the given address to confirm subscription.
*
Step 1
# Include the Autoloader file (see documentation on how to install the php wrapper)
require 'vendor/autoload.php';
use Mailgun\Mailgun;
# Instantiate the client. Use your APIKey
$mgClient = new Mailgun('key-3ax6xnjp29jd6fds4gc373sgvjxteol0l');
# Issue the call to the client.
$result = $mgClient->post("lists", array(
'address' => 'my-user-list#mydomain.org',
'description' => 'A List of users'
));
This creates a list named 'A list of users' and is reachable internally using 'my-user-list#mydomain.org'
** Step 2 **
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Widget</title>
</head>
<body>
<form action="contact.php" method="POST" onsubmit="return check_the_box()">
<input name="email" type="text" required="yes">
I've read and agree to the ToS
<input id="chx" type="checkbox">
<button type="submit" >Submit</button>
<span style="display: none" id="pls"> Please check the Terms of Service box</span>
</form>
<script type="text/javascript">
var box = document.getElementById("chx");
var alrt = document.getElementById("pls");
function check_the_box(){
if(!box.checked) {
alrt.setAttribute("style","display: inline");
return false
}
else {
return true
}
}
</script>
</body>
</html>
Then you need to capture the fields from this form and send an email:
# Include the Autoloader require 'vendor/autoload.php';
use Mailgun\Mailgun;
# Instantiate the client.
$mgClient = new Mailgun('key-3ax6xnjp29jd6fds4gc373sgvjxteol0l');
$domain = "mydomain.org";
# Make the call to the client.
$result = $mgClient->sendMessage($domain, array(
'from' => 'YOU#mydomain.org',
'to' => 'GUYWHOSIGNEDUP#hisdomain.com',
'subject' => 'Confirmation Email',
'html' => 'Please, click this link to confirm you want to be part of my list <a href=\'http://website.com/confirmation.php?authcode=SOMEGENERATEDSTRING\'> CONFIRM </a>'
));
Create a generated string for each email address
The user will click on the link and will be redirected and finally added to the mailing list:
Step 3
Adding to mailing list
# Include the Autoloader (see "Libraries" for install instructions)
require 'vendor/autoload.php';
use Mailgun\Mailgun;
# Instantiate the client.
$mgClient = new Mailgun('key-3ax6xnjp29jd6fds4gc373sgvjxteol0l');
$listAddress = 'my-user-list#mydomain.org';
# Issue the call to the client.
$result = $mgClient->post("lists/$listAddress/members", array(
'address' => 'GUYWHOSIGNEDUP#hisdomain.com',
'description' => 'Signed up',
'subscribed' => true,
));
Done!
All you need to do is create the generation code that is unique to every user, did something like that:
function makeConfirmation($newguy) {
$confirmCode = md5($newguy.'somestringonlyknowntome');
return $confirmCode;
}
And finally before adding him to the mailing list I confirm the parameter sent to my server in the URL when he clicked the confirmation email
function checkConfirmation($conf,$email) {
if($conf== md5($email.'somestringonlyknowntome')) {
return true;
} else { return false; }
}
Edit accordingly..
Additionally:
Here's a good tutorial for you to follow available on Mailgun's github repository in PHP.
Tutorial for OPTins
Good luck and let me know if you succeeded! :)
I'd love to hear from you and write a blog about this on mailgun!
I am trying upload image using rest web service in my symfony application. I have tried the following code but it is throwing the error undefined index photo. I want to know what is the right way to do it.
I have followed how to send / get files via web-services in php but it didn't worked.
Here is the my html file with which am hitting the application url:
<form action="http://localhost/superbApp/web/app_dev.php/upload" enctype='multipart/form-data' method="POST">
<input type="file" name="photo" ></br>
<input type="submit" name="submit" value="Upload">
</form>
And my controller method is like:
public function uploadAction(){
$request = $this->getRequest(); /*** get the request method ****/
$RequestMethod = $request->getMethod();
$uploads_dir = '/uploads';
foreach ($_FILES["photo"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
$tmp_name = $_FILES["photo"]["tmp_name"][$key];
$name = $_FILES["photo"]["name"][$key];
move_uploaded_file($tmp_name, $uploads_dir."/".$name);
}
}
}
If you are using Symfony, you should use Symfony forms to do this. In your example, you put an URL which is pointing to app_dev.php, but that url doesn't work in production mode. In the Symfony cookbook there is an article explaining how upload files, which you should read:
http://symfony.com/doc/current/cookbook/doctrine/file_uploads.html
When you have done this, you can upload images via Rest WebService using the route specified for your action, specifying the Content-Type to multipart/form-data, and the name of the field which you add the image would be something like this package_yourbundle_yourformtype[file].
I have one form text field in index.php and i fill 8080
http_port : <input size="5" name="http_port" value="3128">
and table 'squid' like this
no | tag | value
1 | http_port | 3128
when i click submit button its redirect to submit.php but the table didnt update the value or still 3128.
submit.php
<?php
include("conn.php");
$value = $_POST['http_port'];
$query = mysql_query("update squid set http_port='$value' where '$no'=1");
echo "Update was Succesful<br>
Bac";
?>
whats wrong with my script? Thanks and sorry for my bad english :)
There's a mistake in your mysql_query call, it should be:
$query = mysql_query("update squid set tag ='$value' where no=1");
I haven't coded anything in PHP for ages, but there are plenty of tutorials for such simple MySQL/PHP forms. Code I provided updates tag column, and in similar way you can update other columns...
$query = mysql_query("update squid set value ='$value' where no=1 and tag = 'http_port'");
Try this:
index.html:
<html>
<body>
<form action="submit.php" method="post">
http_port :
<input size="5" name="http_port" value="3128" />
<input type="submit" />
</form>
</body>
</html>
submit.php:
<?php
require("conn.php");
if(isset($_POST['http_port']))
{
$value = $_POST['http_port'];
//You should sanitize data before inserting into table
$value = mysql_real_escape_string(htmlentities(strip_tags(trim($value))));
$no="your table field name";
$sql = "UPDATE squid SET http_port=".$value." where ".$no."=1";
$query = mysql_query($sql) OR die("Err:".mysql_error());
echo "Update was Successful<br />Back";
}
else
{
echo "Something else";
}
?>
$value is in single quotes so will not get the value of variable $value.
$sql = "update squid set http_port='".$value."'where no=1";
Please refer to the following code:
<cfform method="POST" action="#CGI.script_name#">
<p>Enter your Name:
<input name="name" type="text" hspace="30" maxlength="30">
<input type="Submit" name="submit" value="OK">
</cfform>
<cfscript>
function HelloFriend(Name) {
if (Name is "") WriteOutput("You forgot your name!");
else WriteOutput("Hello " & name &"!");
return "";
}
if (IsDefined("Form.submit")) HelloFriend(Form.name);
</cfscript>
Source: http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=UDFs_01.html#1167055
The code runs fine even without CGI.script_name attribute of action field. May I know why it's required then? The description says "Uses the script_name CGI variable to post to this page without specifying a URL. "
The default action of an HTML form is to submit to itself when no action is specified. See this related discussion on the topic: Is it a good practice to use an empty URL for a HTML form's action attribute? (action="")
I always include an action, if for no other reason, to avoid confusion.
I am trying to create a Zend_Controller_Router_Route_Regex route to process URLs in the following form:
search?q=chicken/page=2 where the first regex subpattern would be chicken and second one would be 2. As for the second part where page=2, I want to make it optional if it is the first page, that is page=1. So another url such as search?q=chicken would also be valid and is equivalent to search?q=chicken/page=1.
Here is my attempt albeit without any success, but to give you a better picture of what I am trying to do.
$route = new Zend_Controller_Router_Route_Regex(
'search\?q=([a-zA-Z0-9]+)(?:/page=(\d+))',
array(
'page'=> '1',
'module' => 'default',
'controller' => 'search',
'action' => 'index' ),
array( 1 => 'query', 2 => 'page' ),
'search?=%s/page=%d');
$router->addRoute('search', $route);
The problem here is I cant compose the correct regex.
Thanks in advance.
EDIT #1
The correct regex, as pointed out by MA4, is 'search\?q=([a-zA-Z0-9]+)(?:/page=(\d+))?'
The real problem is pointed by Darryl. Here is a little bit more info to put things into perspective.
My search text box and button
<form action="/search" method="get">
<input type="text" name="q" />
<input type="submit" value="Search" />
</form>
Every time I press the search button, I get the search?q=[text] request. How do I force it to go through the regex match route?
Here is what i want to do, however the code does not work
if($this->getRequest()->getParam('query')){
// redirect success
} else {
$url = "search?q=" . $this->_getParam('q');
$this->_redirect(route('search'), array('code' => 301 ));
}
/search?q=chicken/page=2 is not parsable by Zend Frameworks' router. The router will only see /search.
The router relies on the Path Info provided by the server and anything after the ? is the query string.
You would need to use a path such as this:
/search/[word] (default page 1)
/search/[word]/[page]
In which case your regex would become much simpler.
make the second part optionnal by adding a ? after it:
search\?q=([a-zA-Z0-9]+)(?:/page=(\d+))?