Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Convert a php base api into foxpro

Status
Not open for further replies.

siddeek

Programmer
Jul 13, 2010
4
LK
Dear Experts

I need to do a small automation for one my client. He does online business, as soon as he invoiced a customer, he should communicate this invoice to the courier company. The courier company has send us the following php based api could anyone help me to convert this into foxpro based one.

<?php
// pickup_request function

function pickup_request($api_key,$client_id,$recipient_name,$recipient_contact_no,$recipient_address,$recipient_city,$parcel_type,$cod_amount,$parcel_description,$order_id){

// curl post

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL," curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
"client_id=$client_id&api_key=$api_key&recipient_name=$recipient_name&recipient_contact_no=$recipient_contact_no&recipient_address=$recipient_address&parcel_type=$parcel_type&recipient_city=$recipient_city&parcel_description=$parcel_description&cod_amount=$cod_amount&order_id=$order_id");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
echo $server_output = curl_exec($ch);
curl_close ($ch);

}

//call set parcel variables

$api_key = "apiNNNN0000000bxx";
$client_id = "CLI123";
$recipient_name = "MR ABC";
$recipient_contact_no = "0755555555";
$recipient_address = "No Street, XYZ Region";
$recipient_city = "City";
$parcel_type = "2";
$cod_amount = "10.00";
$parcel_description = "book";
$order_id = "1234";

//call pickup_request function


pickup_request($api_key,$client_id,$recipient_name,$recipient_contact_no,$recipient_address,$recipient_city,$parcel_type,$cod_amount,$parcel_description,$order_id);


}

?>
 
In short the following code translates it, read the comments, though:

Code:
api_key = "apiNNNN0000000bxx"
client_id = "CLI123"
recipient_name = "MR ABC"
recipient_contact_no = "0755555555"
recipient_address = "No Street, XYZ Region"
recipient_city = "City"
parcel_type = "2"
cod_amount = "10.00"
parcel_description = "book"
order_id = "1234"

pickup_request(api_key,client_id,recipient_name,recipient_contact_no,recipient_address,recipient_city,parcel_type,cod_amount,parcel_description,order_id);


Function pickup_request(api_key,client_id,recipient_name,recipient_contact_no,recipient_address,recipient_city,parcel_type,cod_amount,parcel_description,order_id)
   Local loHTTP As MSXML2.XMLHTTP.6.0

   URL = "[URL unfurl="true"]https://courier.cm/api/p_request_v1.02.php"[/URL]
   Action = "POST"
   postbody = Textmerge("client_id=<<client_id>>&api_key=<<api_key>>&recipient_name=<<recipient_name>>&recipient_contact_no=<<recipient_contact_no>>"+;
      "&recipient_address=<<recipient_address>>&parcel_type=<<parcel_type>>&recipient_city=<<recipient_city>>&parcel_description=<<parcel_description>>&cod_amount=<<cod_amount>>&order_id=<<order_id>>")

   loHTTP = Createobject('MSXML2.XMLHTTP.6.0')
   loHTTP.Open(Action, URL , .F.)
   loHTTP.SetRequestHeader('content-type', 'application/x-[URL unfurl="true"]www-form-urlencoded')[/URL]
   Try
      loHTTP.Send(postbody)
   Catch To loExc
      ? loHTTP.Status
      ? loExc.Message
   Endtry

This is based on the documentation of CURL, especially CURLOPT_POSTFIELDS:

Which specifies it is using a Content-Type application/x-
There are many "may"s in that and so the actual request better is checked with a tool like Fiddler when you do the PHP script with PHP. Simply get that and use the PHP code, then mimick the post request as seen from Fiddler.

If you don't know how to use fiddler, I guess there are great forums, also here at tek-tips, that handle the http topic and HTML, XML, PHP, all that stuff isn't VFP and you can't simply translate 1:1.

There is a thing that would make conversion one step easier, because it would remove turning CURL usage to mere XMLHttpRequest usage, the curl library usable in VFP: allows you to use CURL command syntax. It's still not very straight forward to use.

Chriss
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top