thanks for bringing that up... I will look at throughly later..
For the time being you may want to experiment by sending the cURL post request to https://tripletex.no/JSON-RPC . The common response from tripletex if we send an empty data to this url will be
{"error":{"code":590,"msg":"couldn't parse request arguments"}}
That response will enable us to create a cURL function using the proper protocol as described in the documentation.
Without reading the documentation in its entirety, they want us to send request in this format as shown in their JAVA recommendation.. here is an excerpt of the JAVA code..
public static void main(String[] args) throws Exception {
HttpClient httpClient = new HttpClient();
PostMethod method = new PostMethod("https://tripletex.no/JSON-RPC");
String request1 = "{\"method\": \"Sync.login\", \"params\":
[7,\"test246\",\"test@test.no\",\"pwd\"], \"id\": 1}";
What the JAVA code above is requesting a method login, and sending the user password and id to the tripletex.no/JSON-RPC()..
Now, the challenge is how are we going to create the same functionality in PHP.. Keep in mind that tripletex requires us to have the cookies, and not only a cookie, but specifically they want the second cookie from the two outputted by the server.
here is the typical tripletex response along with two cookies and they want the second one..
RESPONSE:
Server: Apache-Coyote/1.1
X-Powered-By: Servlet 2.4; JBoss-4.0.2 (build: CVSTag=JBoss_4_0_2
date=200505022023)/Tomcat-5.5
Set-Cookie: JSESSIONID=09E2172BB69BA99F9CBA3AD9C0F2A025; Path=/; Secure
Set-Cookie: JSESSIONID=F81885A42D1CC3876039EAB8C441C5B3; Path=/; Secure
Content-Type: text/plain;charset=utf-8
Content-Length: 29
Date: Wed, 21 Jul 2010 06:59:38 GMT
Tripletex wants to validate on the second cookie from the response. This is all they want..
Set-Cookie: JSESSIONID=F81885A42D1CC3876039EAB8C441C5B3; Path=/; Secure
Now we already done witht the layout, target url, data types, and requirements. The last thing we need to know about this API is the method of http request.. is it GET, POST or Hybrid? If look at the JAVA code example, we easily see that the JAVA sends the data by post method.
new PostMethod("https://tripletex.no/JSON-RPC");
Now, the fun and excruciating part of being a developer is translating the JAVA example to PHP and utilizing the cURL to tap into the external environment.
**WARNING! **I have not tested this script and this is primarily intended as a building block, this may work and it may NOT. However, the most important of building an application is the building block and we can build from it.. eliminate things that are already proven to be faulty or not functional in the intended environment.
!IMPORTANT! We will attempt to store both cookie first in the cookiejar and lets hope the API will find it acceptable once it knows that the second one is there.
Step 1. Create a directory called cookie, make sure this writtable.. 0777 will do it on the test site but for the production server, it is nice to have it in minimum writtable at 0755 ( it depends if running on fast CGI or not).
Step 2. Layout the cURL connector, while ignoring the Paypal API response. We don't want sending any actual data yet, until we are sure that our application is able to communicate, post, and retrieve information from the remote script.
!IMPORTANT! In the manual, there is terminology called method of invocation. The method of invocation in JAVA for this API is like this
{method": "Sync.login", "params": [7,"pwd","json@test.no","pwd"], "id": 1}
My un-educated guess of the above invocation protocol can be express in PHP as this..
$methodData = array('method'=>'Sync.login','params'=>array(7,'pwd','jason@test.no','pwd'),'id'=>1);
## now we use json_encode function
$methodData = json_encode($methodData);
**!IMPORTANT! **when bridging the Paypal API response, you may need to include the response array.. I believed paypal API response is already in JSON encoded format, so you may not need it here, just use array_pust after the methodData..
here we go... live scripting on a textbox... only in Daniweb :)
## lets setup the target API url
$url = 'https://tripletex.no/JSON-RPC';
## define the cookie directory wher the cookie recieved will be stored
$cookie_bin = 'cookie/cookie.txt'; // as described in step 1
## lets initialize the curl.. if this method does not work you can use the commented option below.
$ch = curl_init($url);
## we might not need this for now
//curl_setopt($ch, CURLOPT_HEADER, 0);
## we define where to store and check for cookies
curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie_bin);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_bin);
## we try this first
curl_setopt($ch, CURLOPT_POSTFIELDS, $methodData);
## if the above does not work, uncomment below
//curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
## we need to witheld the paypal data for now.
//curl_setopt($ch, CURLOPT_POSTFIELDS, $methodData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER,
## we notify the remote server of the content type and the lenght of the entire methodData string
array('Content-Type: application/json',
'Content-Length: ' . strlen($methodData)) );
## finally, we will use either var_dump or print_r to view the respnse of the remote server..
var_dump(curl_exec($ch);
If you get a successfull transaction with the remote server.. you should be able to view some response on the page... to decode them all you have to do is
var_dump(jason_decode(curl_exec($ch));
Step 3 to Step ???? , debug, clean up the codes, test run, until you get the results acceptable to API standards.
!IMPORTANT! You need to read the cURL documentation here.. most importantly this function CURLOPT_CUSTOMREQUEST. Some API does not allow this or will not respond to this method.
That's pretty much it.... that should conclude my semester break... I need to read my textbooks that were collecting dust on my table for 6 weeks now..
Good Luck to you.. I am pretty sure, the script above just needs a minor tweaking and it will be functional.