Click here to Skip to main content
14,268,557 members
Rate this:
Please Sign up or sign in to 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

Rate this:
Please Sign up or sign in to vote.

Solution 1

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[^].
   

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100