简体   繁体   English

C#/ JSON转换为PHP脚本

[英]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. 我需要从网站的REST api中提取数据,但是我从未使用过它,并且一段时间后我又重新开始编码。 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... 但是我需要使这个项目非常迅速地向前发展,并且在我的一生中无法解决,必须弄清楚如何将此C#/ JSON字符串转换为php等效脚本以轮询我需要的数据...

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. 我想我需要为此使用curl,但是很长一段时间以来,我有点生锈。 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. 您可以继续尝试使用cUrl http://php.net/manual/en/book.curl.php ,它用于进行与API不同的调用。

There is also this library which is based on cUrl. 还有一个基于cUrl的库。 https://github.com/php-curl-class/php-curl-class and some samples from the site. https://github.com/php-curl-class/php-curl-class和该站点的一些示例。

//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'] $ _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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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