Click here to Skip to main content
15,881,588 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I need to write the below php code in in vb.net .Can somebody help me?
Is there any code converter from php to vb.net?

<?php
   /**
    * Function to fetch access token
    */
       function get_token($userName, $userPassword) {
            //init the url
            $Url = "http://api.abcdtesting.net/json/token";
            //encapsulate the credentials into a base64 string
            $authString = base64_encode($userName . ":" . $userPassword);

            //init header array
            $header = array();
            $header[] = 'Content-length: 0';
            $header[] = 'Content-type: application/json';
            $header[] = 'Authorization:' . $authString;

            //init curl api and send request - returning $data
            $ch = curl_init();
            $timeout = 5;
            curl_setopt($ch, CURLOPT_URL, $Url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
            $data = curl_exec($ch);

            //process response - return token or assert error
            if (curl_errno($ch)) {
               echo 'error:' . curl_error($ch) . '<br/>';
               curl_close($ch);
            return '';
            } else {
              //token valid
              $tk = json_decode($data, true);
              curl_close($ch);
              return $tk[0]['token'];
            }
        }


What I have tried:

I need to write the below php code in in vb.net .Can somebody help me?
Is there any code converter from php to vb.net? 

<pre><?php
   /**
    * Function to fetch access token
    */
       function get_token($userName, $userPassword) {
            //init the url
            $Url = "http://api.abcdtesting.net/json/token";
            //encapsulate the credentials into a base64 string
            $authString = base64_encode($userName . ":" . $userPassword);

            //init header array
            $header = array();
            $header[] = 'Content-length: 0';
            $header[] = 'Content-type: application/json';
            $header[] = 'Authorization:' . $authString;

            //init curl api and send request - returning $data
            $ch = curl_init();
            $timeout = 5;
            curl_setopt($ch, CURLOPT_URL, $Url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
            $data = curl_exec($ch);

            //process response - return token or assert error
            if (curl_errno($ch)) {
               echo 'error:' . curl_error($ch) . '<br/>';
               curl_close($ch);
            return '';
            } else {
              //token valid
              $tk = json_decode($data, true);
              curl_close($ch);
              return $tk[0]['token'];
            }
        }
Posted
Updated 8-Jun-18 0:13am
Comments
Richard MacCutchan 8-Jun-18 5:42am    
Help with what? This site does not provide code conversion services.

1 solution

There is no converter for such code. Even if there are PHP to VB.Net converters (I don't know or checked), they would not be able to create ready to use code.

You have to understand what the PHP code is doing and wrote VB.Net code providing the same functionality.

Instead of curl use the HttpWebRequest Class (System.Net)[^].

For parsing JSON you can use Json.NET - Newtonsoft[^].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900