Laravel 5.2 Post form to external URL on submit - php

I am using a third party payment service.
<form name="form1" method="post" action="https://exampledomain.com/postpayment.php">
<input type="hidden" name="CRESecureID" value="1"/>
<input type="hidden" name="trans_type" value=" 2"/>
<input type="hidden" name="content_template_url" value="https://example.com/enterpaymentdetails.html"/>
<input type="hidden" name="allowed_types" value=" 3"/>
<input type="hidden" name="total_amt" value="$payment->amount"/>
<input type="hidden" name="collect_total_amt" value="$payment->total"/>
<input type="hidden" name="sess_id" value="e91dd8af53j35k072s0bubjtn7"/>
<input type="hidden" name="sess_name" value="session"/>
<input type="hidden" name="return_url" value="https://example.com/return.html"/>
<p><label> <input type="submit" name="submit" value="submit"/> </label></p>
How can I correctly send these variables to an external URL? Should I do it in the controller? An example would be helpful.
Thanks

Best method to implement this is:
Post form data to one of your own URL, then post this data to the required external URL using Guzzle client or curl request.
This approach will help you to track the response from the external URL and manipulate or use it if required.
Hope this helps.

You can do it on controller, a good approach will be to get these variables on controller, pass them to repository/service layer, perform all the validation checks and once everything is good, send them via CURL POST.
You can use Request to validate the form fields. Validate, process and send.
Here is the code to send data via CURL post
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "URL_OF_API");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 100);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFieldsArray);
$data = curl_exec($ch);
curl_close($ch);

Try adding double forward-slash in your form action
E.g. <form action="{{'//newsite.com/whatever'}}" >
That is all.

Related

Request XML data from other server

I am trying to use Namecheap's API for domain information, although their API documentation doesn't explain very well and it would be the first time that I am using XML data, I'm trying to return the data from the XML file.
I am currently using a form to add variables and POST/GET data to this XML request, on submit, the XML page displays and that is the end of that.
I am wondering how it is possible to send and receive this data using PHP?
Here is what I have so far...
Very Simple:
<form action="https://api.namecheap.com/xml.response" method="POST">
<input type="hidden" id="ApiUser" name="ApiUser" value="user" />
<input type="hidden" id="UserName" name="UserName" value="user" />
<input type="hidden" id="ApiKey" name="ApiKey" value="###" />
<input type="hidden" id="Command" name="Command" value="namecheap.domains.check" />
<input type="hidden" id="ClientIp" name="ClientIp" value="<? echo $_SERVER['REMOTE_ADDR']; ?>" />
<input type="text" id="DomainList" name="DomainList" />
</form>
Like I said, this sends me straight through to the XML file. I am thinking that I probably need to perform a POST ISSET or something...
<?
if(isset($_POST['domain_check'])){
PERFORM XML STUFF HERE
}
?>
Yet I don't have a clue how to send or receive data in this way as I am a beginner. The data is returned like so:
<ApiResponse xmlns="http://api.namecheap.com/xml.response" Status="OK">
<Errors/>
<Warnings/>
<RequestedCommand>namecheap.domains.check</RequestedCommand>
<CommandResponse Type="namecheap.domains.check">
<DomainCheckResult Domain="test.com" Available="false" ErrorNo="0" Description="" IsPremiumName="false" PremiumRegistrationPrice="0" PremiumRenewalPrice="0" PremiumRestorePrice="0" PremiumTransferPrice="0" IcannFee="0" EapFee="0"/>
</CommandResponse>
<Server>PHX01APIEXT01</Server>
<GMTTimeDifference>--4:00</GMTTimeDifference>
<ExecutionTime>1.061</ExecutionTime>
</ApiResponse>
Please inform me with the best way to do this? Thanks.
To parse the response try using simple_xml,
$xml=simplexml_load_string($myXMLData) or die("Error: Cannot create object");
print_r($xml);
Try using cURL so you don't have to submit a form manually, I haven't tested this but this example should help get you going
<?php
$ch = curl_init();
$baseUrl = 'https://api.sandbox.namecheap.com/xml.response?ApiUser=ncuser&ApiKey=apikey&UserName=ncuser&ClientIp=121.22.123.22&Command=namecheap.domains.check&DomainList=domain1.com,domain2.com';
$url = $baseUrl . $query;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$output = curl_exec($ch);
if ($output === false) {
die(curl_error($ch));
}
//print_r(curl_getinfo($ch));
curl_close($ch);
return $output;

Looking at paypal API - What is cURL?

I've been looking at several examples of utilizing paypal's API to develop a simple script to be able to use it to checkout, and in each one I keep running across cURL, and I really don't understand it at all. Is it just different PHP functions from a library, or something all together different? More importantly, can I copy and paste cURL commands into my document and it will work, or do I need to download something to get it work on my local server and shared hosting server where my live site will go? Lastly, where can I go to learn this? I've done some searching but haven't found any solid material on it. I really appreciate any help.
A basic understanding of cURL example from PHP Manual
<?php
$ch = curl_init("http://www.example.com/");
$fp = fopen("example_homepage.txt", "w");
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
?>
PHP Manual
cURL with PayPal API . [Found it on the internet]
<?php
$ch = curl_init();
$clientId = "myId";
$secret = "mySecret";
curl_setopt($ch, CURLOPT_URL, "https://api.sandbox.paypal.com/v1/oauth2/token");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, $clientId.":".$secret);
curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=client_credentials");
$result = curl_exec($ch);
if(empty($result))die("Error: No response.");
else
{
$json = json_decode($result);
print_r($json->access_token);
}
curl_close($ch);
?>
You can use a simple form to the checkout. To use via cURL you need to enable cURL and use it in your PHP code.
The following is the simple way.
<form action="https://paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="business" value="sandy_1314264698_biz#gmail.com ">
<input type="hidden" name="cmd" value="_xclick-subscriptions">
<input type="hidden" name="item_name" value="sand bag">
<input type="hidden" name="item_number" value="1234">
<input type="hidden" name="currency_code" value="USD">
<input type="hidden" name="notify_url" value="http://abc.com/xyz.php">
<input type="hidden" name="return" value="http://google.com">
<!-- Display the payment button. -->
<input type="image" name="submit" border="0" src="btn_subscribe_LG.gif" alt="PayPal - The safer, easier way to pay online">
<img alt="" border="0" width="1" height="1" src="https://www.paypal.com/en_US/i/scr/pixel.gif" >

Send XML from URL on PHP submit

Let's imagine the following situation:
You have a simple php page with an html form that allows the user to enter the following data:
Phone Number
Text Message
Such as:
<form action="xml-file.xml" method="get">
<input type="text" id="to" name="to" value="" />
<input type="text" id="text" name="text" value="" />
<input type="submit" />
</form>
Now, you create an XML using this method that I've tested and works without a problem, and afterwards you want to send it via URL using this other method (CURL).
Unfortunately, I can't reach the CURL method, as I can't link the submit action to both create the XML and send it to an URL so it goes to a local server (www.example.xxx:1234) and afterwards processes that XML for sending an SMS.
That server sends a response if the format of the XML is the right one. My issue is, therefore, on the submission of the created XML.
Help?
Update 1: Adding the updated code. This allows me (with a chmod 777 onto the sms.xml file) to edit the xml file at will.
index.php
<html>
...
<form action="send.php" method="get">
<input type="text" id="to" name="to" value="" />
<input type="text" id="text" name="text" value="" />
<input type="submit">
</form>
...
</html>
send.php
<?php
$xml = simplexml_load_file("sms.xml"); // Load XML file
$xml->Title3 = $_GET['to']; // Updating <Title3> from GET method
$xml->Title5[0]->Title51Content = $_GET['text']; // Updating <Title51> from GET method
$xml->asXML('sms.xml'); // Saving the XML file
?>
sms.xml
<?xml version="1.0" encoding="UTF-8"?>
<Title1>
<Title2>Some Text</Title2>
<Title3>Variable 1</Title3>
<Title4>Some Text</Title4>
<Title5>
<Title51>Variable 2</Title51>
</Title5>
</Title1>
Side note: XML should have, when sending, the "Content-Type","application/x-www-form-urlencoded" header for returning something like this:
XML=%3C%3Fxml+version%3D%221.0%22+encoding%3D%22UTF-8%22%3F%3E%0A%3...
Thanks!
Return the XML as a string and post it?
...
$xml_post_string = 'XML='.urlencode($xml->asXML());
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://theurl.com');
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_post_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);
...

Need help logging to remote site with PHP using cURL

I am trying to log into the following site remotely using cURL but I am having trouble.
http://www.cbssports.com/login
Does anyone know what I am doing wrong? Thank you.
<?
$url = 'http://www.cbssports.com/login';
$fields = array(
'login_form::userid'=>urlencode('USERNAME'),
'login_form::password'=>urlencode('PASSWORD')
);
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
$fields_string = rtrim($fields_string,'&');
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
curl_setopt ($ch,CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);
?>
The from has a lot of hidden fields, there's a good change that one or all of these are required (see trimmed version of the form below).
<form method="post" action="/login/index" name="login_form" id="login_form" >
<input type="hidden" name="dummy::login_form" id="dummy::login_form" value="1">
<input type="hidden" id="form::login_form" name="form::login_form" value="login_form">
<input type="hidden" value="http://www.cbssports.com/login" name="login_form::xurl" id="xurl">
<input type="hidden" value="150" name="login_form::master_product" id="master_product">
<input type="hidden" value="cbssports" name="login_form::vendor" id="vendor">
<input type="text" value="" name="login_form::userid" id="userid" size="30" maxlength="50" data-field-required="1">
<input type="password" value="" name="login_form::password" id="password" size="30" maxlength="12" data-field-required="1">
<input type="submit" value="Sign In" class="formButton">
</form>
If you're using Firefox, I recommend you do a dummy post using LiveHTTPHeaders, just to check all the post fields.
I know this is old, but I was fiddling with the same thing using .NET. Here is what the content string will look like in the POST request (using C# string concatenation):
"dummy%3A%3Alogin_form=1&form%3A%3Alogin_form=login_form&login_form%3A%3Axurl=http%3A%2F%2Fwww.cbssports.com%2Flogin&login_form%3A%3Amaster_product=150&login_form%3A%3Avendor=cbssports&login_form%3A%3Auserid=" + userID + "&login_form%3A%3Apassword=" + password
Notice that the miscellaneous hidden fields will also need to be in there. The additional catch I ran into was that the response has more than one cookie that you'll need for the subsequent GET request. Thus, you'd need to use some kind of container rather than appending a single "Set-cookie" header. That will not work and your output stream will simply return the data from the main page you're redirected to.
Hope that helps someone.

Generate paypal payment links with amout and recipient

I need to dinamically generate PayPal links with customized payment amounts and recipients. I don't know much about PayPal's way of handling this kind of stuff, and I actually wonder whether it is possible. I have a table with user's email address and payment value and, on the last column, I need to put that link that, clicked, takes to the PayPal's page where, after security checks and everything, the amount of money by me given can be directly transferred to the email address of the user.
Is it possible to do something like this? I'm working in PHP.
Thanks.
please, read documentation - it's all very well described. you will need to register at https://developer.paypal.com/; refer to https://www.paypal.com/documentation for more information.
I believe paypal standard payments would suffice your needs. read this https://cms.paypal.com/cms_content/US/en_US/files/developer/PP_WebsitePaymentsStandard_IntegrationGuide.pdf -- it has all sorts of samples.
when we integrate paypal, we usually do like this: user picks goods to purchase, then proceeds to checkout. at this point, form is created as per documentation, and that form includes merchant account to use for processing the payment. in the form you also specify: notify_url and cancel_url, so you would get notification from the paypal as to what happened and to which account. that should address your needs.
sample form:
<form action="https://www.'.$this->isSandbox().'paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="business" value="'.$this->api->getConfig('billing/paypal/merchant').'">
<input type="hidden" name="cmd" value="_xclick">
<input type="hidden" name="rm" value="2">
<input type="hidden" name="item_name" value="'.addslashes($_GET['descr']).'">
<input type="hidden" name="item_number" value="'.addslashes($_GET['id']).'">
<input type="hidden" name="amount" value="'.addslashes($_GET['amount']).'">
<input type="hidden" name="no_shipping" value="1">
<input type="hidden" name="notify_url" value="http://' . $_SERVER['HTTP_HOST'].$this->api->getDestinationURL('/ppproxy',array('ipn'=>$_GET['id'])).'">
<input type="hidden" name="cancel_return" value="http://' . $_SERVER['HTTP_HOST'].$this->api->getDestinationURL('/ppproxy',array('cancel'=>$_GET['id'])).'">
<input type="hidden" name="return" value="http://' . $_SERVER['HTTP_HOST'].$this->api->getDestinationURL('/ppproxy',array('success'=>$_GET['id'])).'">
<input type="hidden" name="currency_code" value="'.addslashes($_GET['currency']).'">
</form>
sample callback handler:
....
if($_POST){
// might be getting adta from paypal! better log!
foreach ($_POST as $key=>$value) $postdata.=$key."=".urlencode($value)."&";
$postdata.="cmd=_notify-validate";
$curl = curl_init("https://www.".$this->isSandbox()."paypal.com/cgi-bin/webscr");
curl_setopt ($curl, CURLOPT_HEADER, 0);
curl_setopt ($curl, CURLOPT_POST, 1);
curl_setopt ($curl, CURLOPT_POSTFIELDS, $postdata);
curl_setopt ($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt ($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($curl, CURLOPT_SSL_VERIFYHOST, 1);
$response = curl_exec ($curl);
curl_close ($curl);
$this->api->logger->logLine($response);
if ($response != "VERIFIED"){
$this->api->logger->logLine('FAILED: post='.print_r($_POST,true));
exit;
}else{
$this->api->logger->logLine('VERIFIED: post='.print_r($_POST,true));
}
if($_POST['payment_status']=='Completed' and $_POST['txn_type']!='reversal')return true; // or perform your desired actions
exit;
}
excerpt from agiletoolkit.org paypal billing addon

Categories