I am trying to create a form for the user to buy product from my Ruby on Rails website by using their "Scratch Card for Mobile Phones".
The problem is that the service only provides Module code for PHP. So I have to convert it to Ruby to put into my website. Here are the codes I want to convert to Ruby:
$post_field = 'xyz=123&abc=456';
$api_url = "https://www.nganluong.vn/mobile_card.api.post.v2.php";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$api_url);
curl_setopt($ch, CURLOPT_ENCODING , 'UTF-8');
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_field);
$result = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$error = curl_error($ch);
I have tried to convert them to Ruby code but always got confused. Can anyone help me convert these codes into working Ruby codes? Thanks in advance!
Here is my silly code so far:
RestClient.post($api_url, $post_field, "Content-Type" => "application/x-www-form-urlencoded")
Basically all that I need is a ruby version of the php curl code. I'm a newbie, not an experienced programmer, so please help.
Try something like this:
require 'rest-client'
require 'rack'
post_query = 'xyz=123&abc=456'
api_url = "https://www.nganluong.vn/mobile_card.api.post.v2.php"
query_hash = Rack::Utils.parse_nested_query(post_query)
begin
response = RestClient.post api_url, :params => query_hash
print response.code
print response.body
rescue Exception => e
print e.message
end
All the code is doing is sending a payload xyz=123&abc=456 through a POST request to a specified URL.
You might use e.g. the curb gem for this:
response = Curl.post("https://www.nganluong.vn/mobile_card.api.post.v2.php", {:xyz => 123, :abc => 456})
result = response.body_str
status = response.status
Related
I'm working on Bus API of arzoo. The server must receive the posted data in simple POST Request. To achieve this i'm using PHP cURL. In the API Document it is clearly mention that the data should be sent in the following format:
<BusRequest>
<source>HYDERABAD</source>
<dest>BANGALORE</dest>
<dep_date>25-Mar-2016</dep_date>
<PartnerId>ID of the partner given by arzoo</PartnerId>
<Clientid>ID of the client given by arzoo</Clientid>
<Clientpassword>Client password given by arzoo</Clientpassword>
<Clienttype>ArzooBUSWS1.0</Clienttype>
</BusRequest>
My PHP cURL code is as follows:
$input_xml = '<BusRequest>
<source>Ernakulam</source>
<dest>Kozhikode</dest>
<dep_date>15-Nov-2017</dep_date>
<PartnerId>partner</PartnerId>
<Clientid>clientid</Clientid>
<Clientpassword>password</Clientpassword>
<Clienttype>clienttype</Clienttype>
</BusRequest>';
$url = "url";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS,
"xmlRequest=" . $input_xml);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 300);
$data = curl_exec($ch);
curl_close($ch);
I'm not getting any valid xml as response.Am i doing anything wrong ?. Your reply will be appreciated. Thank You.
I want send post request from php to python and get answer
I write this script which the send post
$url = 'http://localhost:8080/cgi-bin/file.py';
$body = 'hello world';
$options = array('method'=>'POST',
'content'=>$body,
'header'=>'Content-type:application/x-ww-form-urlencoded');
$context = stream_context_create(array('http' => $options));
print file_get_contents($url, false,$context);
I'm use custom python server
from http.server import HTTPServer, CGIHTTPRequestHandler
server_address = ("", 8080)
httpd = HTTPServer(server_address, CGIHTTPRequestHandler)
httpd.serve_forever()
And python script which the takes post request
print('Content-type: text/html\n')
import cgi
form = cgi.FieldStorage()
text2 = form.getfirst("content", "empty")
print("<p>TEXT_2: {}</p>".format(text2))
And then I get
write() argument must be str, not bytes\r\n'
How can it be solved?
P.S Sorry for my bad english
Check curl extension for php http://php.net/manual/en/book.curl.php
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://localhost:8080/cgi-bin/file.py");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
"postvar1=value1&postvar2=value2&postvar3=value3");
// in real life you should use something like:
// curl_setopt($ch, CURLOPT_POSTFIELDS,
// http_build_query(array('postvar1' => 'value1')));
// receive server response ...
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
curl_close ($ch);
You can also use a library like guzzle that may have some other bells and whistles you may want to use.
Example usage can be found on this other answer here:
https://stackoverflow.com/a/29601842/6626810
When I try to add an image to a product I don't get any errors, but the image doesn't get added.
This is my code:
function addImage($idProduct)
{
$key = 'XXXXXXXXXXXXXXXXXXX';
$url = "http://192.168.1.81/api/images/products/".$idProduct;
$image_path = 'image2.jpg';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_USERPWD, $key);
curl_setopt($ch, CURLOPT_POSTFIELDS, array('image' => '#'.$image_path.';type=image/jpg'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
echo '<h2>Image Added</h2>';
}
I also made a change on PSWebServiceLibrary.php, because Prestashop Web Service API keeps asking for authentication. This is the link where I got the code Prestashop Web Service API keeps asking for authentication .
This is the code I added:
$url .= '&ws_key=' . $this->key;
The problem is that the code to add an image was working before I made that change on PSWebServiceLibrary.php, and I don't know how to solve it.
I am using prestashop 1.6.1.5
Any help will be appreciated.
Greetings!
I think it's only a missing of "?" in your url.
If I follow your $url logic the result will be for product of ID 1234567 by example :
http://192.168.1.81/api/images/products/1234567&ws_key=ZOEJFD3429JD209AZJX0DJF20
So your server waits this url to hanlde ws_key as GET parameter :
http://192.168.1.81/api/images/products/1234567?ws_key=ZOEJFD3429JD209AZJX0DJF20
You need to add this "?" at the end of you URL like this :
$url = "http://192.168.1.81/api/images/products/".$idProduct."?";
Best Regards,
TGA
For me works like this:
curl_setopt($ch, CURLOPT_POSTFIELDS, array('image' => curl_file_create($image_path)));
I am trying to implement 3d secure. I have a xml containing some data as required by 3d secure.Like :
<CardinalMPI>
<MsgType>cmpi_lookup</MsgType>
<Version>1.7</Version>
<ProcessorId>xxx</ProcessorId>
<MerchantId>xxxxxx</MerchantId>
<TransactionPwd>xxxxxxxxxxx</TransactionPwd>
<TransactionType>C</TransactionType>
<Amount>56999</Amount>
<CurrencyCode>xxxxx</CurrencyCode>
<OrderNumber>xxxx</OrderNumber>
<CardNumber>xxx</CardNumber>
<CardExpMonth>xxx</CardExpMonth>
<CardExpYear>xxx</CardExpYear>
</CardinalMPI>
Then how can I request to http://msgtest.bankserv.co.za/maps/txns.asp with above xml and get their response back ? I tried curl, Soap etc and got error. Please help me with some answers in detail.
I have tried the following codes. Please let me know if this code is not correct.
$writer = new XMLWriter();
$writer->openMemory();
$writer->startElement("CardinalMPI");
$writer->writeElement("MsgType","cmpi_lookup");
$writer->writeElement("Version","1.7");
$writer->writeElement("ProcessorId","xxxxx");
$writer->writeElement("MerchantId","xxxxxxx");
$writer->writeElement("TransactionPwd","xxxxx");
$writer->writeElement("TransactionType","C");
$writer->writeElement("Amount",$xxxx);
$writer->writeElement("CurrencyCode","xxxx");
$writer->writeElement("OrderNumber","xxxxxxxxxx");
$writer->writeElement("CardNumber","'xxxxxxxxx");
$writer->writeElement("CardExpMonth","xx");
$writer->writeElement("CardExpYear","xxxx");
$writer->endElement();
$writer->endElement();
$writer->endDocument();
$request = $writer->outputMemory(true);
$serviceArguments = array("validateRequest"=>"0","protocol"=>"v_xml","protocolVersion"=>"2.0","request"=> $request);
$client = new SoapClient("msgtest.bankserv.co.za/maps/txns.asp", array('local_cert'=> "certificate.pem"));
$result = $client->Execute($serviceArguments);
$xml=$result->ExecuteResult;
When I am running this code I am getting exceptions.
For posting values we can make use of CURL in php. Here you can use :
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_URL, "your url");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "yr_variable=".$your xml);
$result=curl_exec($ch);
curl_close($ch);
For testing where should i give the url of the site, can you show me with the example in the above code?
$r = new HTTPRequest("server.php", HTTP_METH_POST);
$r->addPostFields(array('omg' => 'wtf'));
$r->send();
var_dump($r->getResponseCode());
var_dump($r->getResponseBody());
Simply use addHeaders().
The XMLHttpRequest is the value of the X-Requested-With header, so you just have to do:
$r = new HTTPRequest("http://mywebservices.com/somewebserver.php", HTTP_METH_POST);
$r->addHeaders(array('X-Requested-With' => 'XMLHttpRequest'));
Instructions for installing HTTP from PECL: http://www.php.net/manual/en/http.setup.php
phpdev has answered your question very well, and if you install the HTTP extension and do it as according to the answer it will surely work.
$r = new HttpRequest('http://your-required-url-here', HttpRequest::METH_POST);
$r->addPostFields(array('omg' => 'wtf'));
echo $r->send()->getBody();
The urls given by you in the previous comments are not well formed, it seems that you have pasted some random URL.
This is just another way to do it with php CURL.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.openfirms.com/index.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, true);
$data = array('omg' => 'wtf');
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$output = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
print_R($output);
This will output the contents of specified url. Hope it will help