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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Payment provider example in php (needed in asp)

Status
Not open for further replies.

raphael232

Programmer
Jun 9, 2006
51
0
0
GB
Hi, a recent client has requested to use a payment provider i have not used before. I have checked their documentation but all their examples are in php.

Here is the code in their example:

Code:
$poststring = "<?xml version='1.0' encoding='UTF-8'?>....................";

$fp = fsockopen("ssl://".$host, $port, $errno, $errstr, 5);

if(!$fp){
  //error; tell us
  echo "Error: $errstr ($errno)\n";
}else{
  //send the server request
  fputs($fp, "POST $path HTTP/1.0\r\n");
/////////////////////////////////////////////////////////////////////////
// if problem with php 5 use instead
/////////////////////////////////////////////////////////////////////////
//  fputs($fp, "POST $path HTTP/1.0\r\n");
/////////////////////////////////////////////////////////////////////////

  fputs($fp, "Host: $host\r\n");
  fputs($fp,"Content-type: text/xml\r\n");
  fputs($fp, "Content-length: ".strlen($poststring)."\r\n");
  fputs($fp, "Authorization: Basic ".base64_encode($login.":".$pass."\r\n"));
  fputs($fp, "Connection: close\r\n");
  fputs($fp,"\r\n");
  fputs($fp, $poststring . "\r\n\r\n");

  // prepare for reading the response
  stream_set_timeout($fp,5);
  // here we save the response body - XML response from WireCard
  $output = "";
  // here we store the HTTP headers
  $headers= "";
  // temp. variable for detecting the end of HTTP headers.
  $is_header = 1;
  while(!feof($fp)) {
    $buffer = fgets($fp, 128);
    // fgets on SSL socket 
    if ($buffer == FALSE) {
    	break;
    }
    if (!$is_header) {
      $output .= $buffer;
    }
    if ($buffer == "\r\n") {
    	$is_header = 0;
    }
    if ($is_header) {
      $headers .= $buffer;
    }
  }
  //close fp - we are done with it
  fclose($fp);

  // print the results in Web Browser - and convert all special 
  // characters (like <,>,...) to HTML entities
  echo "<PRE>\n";
  echo htmlentities($headers);
  echo "\n";
  echo htmlentities($output);
  echo "</PRE>\n";
}

I'm not too familiar with php and would appreciate if someone could explain to me what this is doin and an point me in the direction of an alternative in asp.

Thanks
 
Basically the above script is submitting some information ($poststring) via SSL to another webserver. It is building the HTTP request headers manually, I guess so it can include the authentication information ($login.":".$pass).

Once it finishes "writing" all of the content it then tries to read in a response from the server. Since it is using a file pointer and only reading in 128 Bytes ata time, it has to continue looping until it reaches the EOF on the response, thus the while(!feof($fp)). Afterward it closes the file handle for the response and prints everything to the browser.

This entire chunk of code could be duplicated using an XMLHTTP object in ASP. The two things you need to look out for are the fact that your URL will probably need to start with HTTPS:// and that you will probably need to add the header for the authentication method.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top