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.
Related
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;
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.
My problem is related to this specific error:
I'm doing a form to contain data that have to be passed to the server to obtain the token, but probably i haven't understood some concepts about doing this
Here is my form
<form method="post" action="https://api-3t.sandbox.paypal.com/nvp">
<input type="hidden" name="USER" value="Apiuser"/>
<input type="hidden" name="PWD" value="Apipass"/>
<input type="hidden" name="SIGNATURE" value="Apisignature"/>
<input type="hidden" name="VERSION" value="52.0"/>
<input type="hidden" name="PAYMENTACTION" value="Sale"/>
<input name="AMT" value="19.95"/>
<input type="hidden" name="RETURNURL" value="http://www.YourReturnURL.com"/>
<input type="hidden" name="CANCELURL" value="http://www.YourCancelURL.com"/>
<input type="image" name="METHOD" src="https://www.paypal.com/it_IT/IT/i/btn/btn_xpressCheckout.gif" value="SetExpressCheckout"/>
</form>
And here is my function
if (isset($_POST['METHOD'])) {
$API_UserName = urlencode("Apiuser");
$API_PassWord = urlencode("Apipass");
$API_Signature = urlencode("Apisignature");
$return_url = "http://www.myurl.com";
$cancel_url = "http://www.myurl.com";
$version = urlencode('52.0');
$nvpreq = "USER=$API_UserName&PWD=$API_PassWord&SIGNATURE=$API_Signature&VERSION=$version&PAYMENTACTION=Sale&AMT=19.95&RETURNURL=$return_url&CANCELURL=$cancel_url&METHOD=SetExpressCheckout";
}
Is seems that something is wrong, can you give me a tip please?
Thanks a lot
Have you checked that you are not encoding the parameters twice? Im my case that was happening though http_build_query() and some default setting which was unstable for some reason due to being changed somewhere during runtime.
Basically your final call should contain & and not &, for me the final curl call for some reason ended up with being encoded as &.
Make sure you are not encoding twize, and if using http_build_query() update your function so it will be explicit on the encoding part:
from: http_build_query($requestParameters)
to: http_build_query($requestParameters,'','&')
The difference is that the last version will explicitly use & as delimiter while the first will use the default, so be sure.
In my website, I am submitting a html form to a payment site (webmasterchecks.com) and the site responses me by XML response. The XML response is displaying on external link (https://api.webmasterchecks.com/payments/add) after normal form submission(by clicking on submit button). I want to read the XML response and update my database. My coding language is PHP.
HTML form:
<form name="payment_form" action="https://api.webmasterchecks.com/payments/add" method="post">
<input type="text" name="client_id" value="****"></input>
<input type="text" name="akey" value="**********"></input>
<input type="text" name="method_id" value="2"></input>
<input type="text" name="payee" value="ABCD EFGH"></input>
<input type="text" name="amount" value="1.00"></input>
<input type="text" name="postage_id" value="6"></input>
<input type="text" name="reference" value="Payment"></input>
<input type="text" name="street_addr" value="51 ABCD"></input>
<input type="text" name="city" value="XYZ"></input>
<input type="text" name="state" value="NJ"></input>
<input type="text" name="country" value="United States"></input>
<input type="text" name="zip" value="01544"></input>
<input type="submit" name="submit" value="submit"></input>
</form>
I tried to submit the form using jquery post and get the json data. But The form is not submitting (not getting any error in console as well). Code is given below.
$(function(){
$("form[name=payment_form]").submit(function(){
$.post($(this).attr("action"), $(this).serialize(), function(jsonData){
alert(jsonData);
}, "json");
return false;
});
});
I also tried to use curl, but did not succeed. Its displaying the API key is invalid where as using the above html form, i am getting response. CURL code is given below.
$client_id = "****";
$api_key = "*****************************";
$output_url = "https://api.webmasterchecks.com/payments/add";
$output_transaction = "client_id=$client_id&";
$output_transaction .= "akey=$api_key&";
$output_transaction .= "method_id=2&";
$output_transaction .= "payee=ABCD EFGH&";
$output_transaction .= "amount=1.00&";
$output_transaction .= "postage_id=6&";
$output_transaction .= "reference=Payment&";
$output_transaction .= "street_addr=FGHFHGFG&";
$output_transaction .= "city=JHGJHG&";
$output_transaction .= "state=NJ&";
$output_transaction .= "country=United States&";
$output_transaction .= "zip=54545454";
ob_start();
$ch = curl_init ($output_url);
curl_setopt ($ch, CURLOPT_VERBOSE, 1);
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $output_transaction);
curl_exec ($ch);
curl_close ($ch);
$process_result = ob_get_contents();
ob_end_clean();
How can i update my database using the XML response?
Thanks in advance.
EDIT
My api key contains few plus "+" sign on it. Sample format of API key is given below.
q5Un5ObLZs2ovY2COW+LvHzEVdUy0kR3u6dPwh/p+wzlbh80vONBbo+otLpBwPqnvP/VjhglfFos51sLFDpUHi+6GnVbLtR3ATjSz9trGoKLFgrK/ostPUG4t9XV1EdS10JzVZFscIxUu2LVmJN9NVpCgaD9NaA
What you will need to do here is send your data through to a local script and then package it up into a curl request to interact with the remote server via its API.
<form name="payment_form" action="local_script.php" method="post">
<input type="text" name="method_id" value="2"></input>
<input type="text" name="payee" value="ABCD EFGH"></input>
<input type="text" name="amount" value="1.00"></input>
<input type="text" name="postage_id" value="6"></input>
<input type="text" name="reference" value="Payment"></input>
<input type="text" name="street_addr" value="51 ABCD"></input>
<input type="text" name="city" value="XYZ"></input>
<input type="text" name="state" value="NJ"></input>
<input type="text" name="country" value="United States"></input>
<input type="text" name="zip" value="01544"></input>
<input type="submit" name="submit" value="submit"></input>
</form>
Note I have removed client_id and api_key from the form as these should never be shown to the public like this
local_script.php
$api_url = "https://api.webmasterchecks.com/payments/add";
$client_id = "****";
$api_key = "*****************************";
$_POST['client_id'] = $client_id;
$_POST['akey'] = $api_key;
$post_data = http_build_query($_POST);
$xml = new DOMDocument();
$ch = curl_init ($api_url);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);//return xml
curl_setopt($ch, CURLOPT_HEADER, FALSE);//we only need the body
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
$xml->loadXML(curl_exec($ch));
curl_close ($ch);
//for testing echo xml
echo $xml->saveXML();
//you need now to parse the xml before adding info to db
$name = $xml->getElementsByTagName('name')->item(1)->nodeValue;
$email = $xml->getElementsByTagName('email')->item(1)->nodeValue;
This is how I would do it, as I am versed in DOMDocument for parsing and writing xml,you can either parse using the manual for DOMDocument http://www.php.net/manual/en/class.domdocument.php or use SIMPLEXML http://php.net/manual/en/book.simplexml.php either way you need to parse the xml to add the data to your db.
Probably you also can send and fetch data to/from that URL using ajax.
I have created simple page which displays map and markers data. I have fetched
the data using ajax. The response to my ajax call was xml. So after
getting response, I have simply parsed the xml.
E.g.
var url="mapData.php";
var request = getRequestObj();
request.open("GET", url, true);
request.onreadystatechange = function() {
if (request.readyState == 4) {
var xmlDoc = request.responseXML;
// obtain the array of markers and loop through it
var markers = xmlDoc.documentElement.getElementsByTagName("marker");
for (var i = 0; i < markers.length; i++) {
//parsing other xml element...
}
}
}
request.send(null);
Hope this would help.
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);
...