简体   繁体   中英

C#/JSON conversion to PHP script

I need to extract data from a website's REST api, but I've never worked with it, and I'm just getting back into coding again after some time. But I need to get this project moving in a forward direction very quickly, and cannot for the life of me, figure out how to convert this C#/JSON string into a php equivalent script to poll for the data I need...

HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(address);
httpWebRequest.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(string.Format("{0}:{1}",UserName, Password))));

I'm thinking I need to use curl for this, but again, it's been a long time, and I'm a little rusty. Any help is greatly appreciated.

You can go ahead and try to use cUrl http://php.net/manual/en/book.curl.php which is used to make calls with the different to an API.

There is also this library which is based on cUrl. https://github.com/php-curl-class/php-curl-class and some samples from the site.

//For making a GET request
$curl = new Curl();
$curl->get('https://www.example.com/');

//For making a POST request
$curl = new Curl();
$curl->post('https://www.example.com/login/', array(
  'username' => 'myusername',
  'password' => 'mypassword',
));  

//With custom headers
$curl = new Curl();
$curl->setBasicAuthentication('username', 'password');

Simple way with $_GET['pass'], $_GET['user']

WebClient webClient = new WebClient();
webClient.Encoding = Encoding.UTF8;
response = webClient.DownloadString("https://example.com/code.php?pass=password&user=username");
Console.Write(response);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM