2016-08-24 114 views
-5

我在使用另一臺服務器上創建的Web服務時遇到了一個非常奇怪的問題。從Web服務獲取空白數組

這是Web服務,我創建

http://52.53.227.143/API_test.php?post_name=BSN%20Syntha-6%20Isolate

我不能能夠數組或我的網站上的任何其他格式接收數據的URL。我同時使用file_get_content和curl來接收json格式,但它只給了我空白數組。

這裏是我的代碼:

$post_name = 'BSN Syntha-6 Isolate'; 
$base = "http://52.53.227.143/API_test.php?post_name=".$post_name; 
$str = file_get_contents($base); 
echo '<pre>'; 
print_r(json_decode($str)); 

它給我下面的結果:

stdClass Object 
(
[info] => Array 
    (
    ) 

) 

我也用捲曲,下面的代碼:

$curl = curl_init(); 
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); 
curl_setopt($curl, CURLOPT_HEADER, false); 
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); 
curl_setopt($curl, CURLOPT_URL, $base); 
curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3'); 
curl_setopt($curl, CURLOPT_REFERER, 'http://www.google.com'); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); 
$str = curl_exec($curl); 
curl_close($curl); 
echo '<pre>'; 
print_r(json_decode($str)); 

請幫助我。

+0

顯示腳本的實際代碼吧? 「儘快」在這裏也不是正確的心態。 – Jakumi

+0

我沒有看到任何代碼。 – Jakumi

+0

我轉發了我的問題。 –

回答

0

使用urlencode()來

這是現在的工作:

<?php 
    $post_name = 'BSN Syntha-6 Isolate'; 
    $base = "http://52.53.227.143/API_test.php?post_name=" . urlencode($post_name); 
    $str = file_get_contents($base); 
    echo '<pre>'; 
    print_r(json_decode($str)); 
?> 
+0

感謝他,現在正在使用curl和file_get_content()。非常感謝 –

+0

不是問題:)如果您已經得到答案,您現在可以將問題標記爲已解決! – mrid