2017-04-15 142 views
2

我有這個問題,PHP和Laravel。我想讀取遠程ULR一個JSON文件:PHP從遠程URL讀取JSON文件

https://services.realestate.com.au/services/listings/search?query={"channel":"buy","filters":{"propertyType":["house"],"surroundingSuburbs":"False","excludeTier2":"true","geoPrecision":"address","localities":[{"searchLocation":"Blacktown, NSW 2148"}]},"pageSize":"100"}

我使用的代碼:

$re_url = 'https://services.realestate.com.au/services/listings/search?query={"channel":"buy","filters":{"propertyType":["house"],"surroundingSuburbs":"False","excludeTier2":"true","geoPrecision":"address","localities":[{"searchLocation":"Blacktown, NSW 2148"}]},"pageSize":"100"}'; 

$ch = curl_init($re_url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
$re_str = curl_exec($ch); 
curl_close($ch); 
$re_list = json_decode($re_str); 

它不停地接收錯誤「在處理您的請求時出錯,參考號30.96464868 .1492255689.1829cf2「

我嘗試了使用」https://google.com.au「的網址,它工作正常,所以它看起來像是URL編碼問題。但我不確定。

任何人都可以幫忙,或有相同的問題?

感謝

回答

2

我相信你有兩個問題,我的代碼將在下面解決它們。我的代碼還採用了一些不同的方法來JSON的避免手工裝配,URL查詢字符串,等等(見行所提供的代碼的3-41)

問題

  1. 你是不是對查詢參數值進行編碼 - 可以用參數值的urlencode固定,但我更喜歡http_build_query,原因在於我的介紹性段落中提到的原因。
  2. 你沒有發送用戶代理(UA)頭(遠端似乎需要在這個頭的值,但不在乎它是什麼。收到一個與UA的請求我認爲它必須白名單IP因爲它似乎並不需要每次請求,但我只是將它發送給每個請求,因爲它不會造成傷害,並且您永遠不知道您的白名單何時會超時)。見50-53行什麼,我在這個腳本設置和一些選項你有

替換代碼

解釋性意見

<?php 

/* 
* The data that will be serialized as JSON and used as the value of the 
* `query` parameter in your URL query string 
*/ 
$search_query_data = [ 
    "channel" => "buy", 
    "filters" => [ 
     "propertyType" => [ 
      "house", 
     ], 
     "surroundingSuburbs" => "False", 
     "excludeTier2" => "true", 
     "geoPrecision" => "address", 
     "localities" => [ 
      [ 
       "searchLocation" => "Blacktown, NSW 2148", 
      ], 
     ], 
    ], 
    "pageSize" => "100", 
]; 

/* 
* Serialize the data as JSON 
*/ 
$search_query_json = json_encode($search_query_data); 

/* 
* Make a URL query string with a param named `query` that will be set as the 
* JSON from above 
*/ 
$url_query_string = http_build_query([ 
    'query' => $search_query_json, 
]); 

/* 
* Assemble the URL to which we'll make the request, and set it into CURL 
*/ 
$request_url = 'https://services.realestate.com.au/services/listings/search?' . $url_query_string; 

$ch = curl_init($request_url); 

/* 
* Set some CURL options 
*/ 
// Have `curl_exec()` return the transfer as a string instead of outputting 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
// Set a user agent header 
curl_setopt($ch, CURLOPT_USERAGENT, 'H.H\'s PHP CURL script'); 
// If you want to spoof, say, Safari instead, remove the last line and uncomment the next: 
//curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/603.1.30 (KHTML, like Gecko) Version/10.1 Safari/603.1.30'); 

/* 
* Get the response and close out the CURL handle 
*/ 
$response_body = curl_exec($ch); 
curl_close($ch); 

/* 
* Unserialize the response body JSON 
*/ 
$search_results = json_decode($response_body); 

最後,順便說一句,我會建議您停止直接使用CURL並開始使用庫來抽象部分HTTP交互,並使您的請求/響應開始適合「標準」(PSR)接口。由於您使用的是Laravel,因此您已經處於一個使用Composer的生態系統中,因此您可以輕鬆地使用install something like Guzzle

+1

非常感謝,精彩的工作。 –

+1

試過Guzzle,太棒了。更容易和更少的編碼。 –

+0

太棒了!我很高興你檢查出來了! – JAAulde

0

我想這裏可能有2個問題,你需要克服

第一個是很簡單的,你需要後來urlencode()「進行搜索?」 一些特殊字符,只是不能直接通到URL

第二個問題,用urlencode後,如果仍然沒有工作,是 您要發送到服務的方式,我有一些API做了,當他們嘗試發送Json的時候,他們通常會把它放在body中,然後「POST」它們,可能你不應該把它們傳遞給url(從開始),當然,如果它仍然不起作用,你只需要考慮它。

先嚐試鍍鉻郵差,你會愛上它的。

+0

剛試過urlencode(),不起作用,錯誤是「您沒有訪問/服務/列表/搜索服務器上的權限」。我在postman中粘貼了原始URL,在那裏工作得很好。 –

+0

所以它在瀏覽器中工作,在郵遞員工作,但不是從PHP代碼。 –

+0

你不想'urlencode'整個查詢字符串,只是參數值。在你給出的URL示例中,如果在查詢=' – JAAulde

0
$_curl = curl_init(); 
curl_setopt($_curl, CURLOPT_SSL_VERIFYHOST, 2); // you missing this line 
curl_setopt($_curl, CURLOPT_SSL_VERIFYPEER, false); // you missing this line 
curl_setopt($_curl, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($_curl, CURLOPT_FOLLOWLOCATION, true); 
curl_setopt($_curl, CURLOPT_URL, $re_url); 
$rtn = curl_exec($_curl);