2017-02-28 165 views
0

我需要從網站的REST api中提取數據,但我從來沒有使用它,並且在一段時間後我再次回到編碼中。但我需要讓這個項目很快向前移動,並且不能爲我的生活,弄清楚如何將這個C#/ JSON字符串轉換成一個php等效腳本來輪詢我需要的數據...將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)))); 

我想我需要使用捲曲來做這件事,但是,這已經很長時間了,而且我有點生疏。任何幫助是極大的讚賞。

回答

0

您可以繼續嘗試使用cUrl http://php.net/manual/en/book.curl.php,它用於使用不同於API的調用。

還有一個基於cUrl的庫。 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'); 
+0

這引起了我很多接近我所需要的,但我仍然有問題的用戶名/密碼編碼。現在,我不得不在C#編譯器中對密碼進行編碼,並將其編碼到我的腳本中。如果任何人都可以弄清楚如何解決這個問題,我將非常感激。 –

0

與$ _GET簡單方法[ '通'],$ _GET [ '用戶']

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

嗯... 1)不要在URL中傳遞密碼,2)我需要將用戶名+密碼發送給別人的服務器,而不是我自己的,並且必須在發送到服務器的HEADER信息中實際上讓我困惑) –

+0

服務器所有者可以看到你的標題,如果想要的話。對於你不信任託管或服務器提供商時使用的服務器:)這很愚蠢。您可以免費打開https://www.openshift.com vps(app)。 1)如果你安裝ssl沒關係。 – 2017-03-01 07:42:47