2015-08-28 138 views
1

我試圖通過curl從服務器獲取數據,但授權後我收到一個空的響應。這個服務器上的站點使用AJAX來顯示內容,所以首先我檢查了通過瀏覽器與它交互時發送的頭文件和查詢參數。空cURL json響應

總標題:

Remote Address:*.*.*.*:80 
Request URL:http://example.com/search/search/?0.42697851033881307 
Request Method:POST 
Status Code:200 OK 

表單數據:

QUERY:29061 
QUERY_TYPE:2 
QUERY_DATA:S1 
PKW:X 
LKW:X 
FORMAT:json 
LANG:ru 

下面是無盡的谷歌搜索和stackoverflowing結果:

<?php 
$curl = curl_init('http://example.com/authorization/login/'); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($curl, CURLOPT_COOKIEJAR, 'cookie.txt'); 
curl_setopt($curl, CURLOPT_COOKIEFILE, 'cookie.txt'); 
curl_setopt($curl, CURLOPT_REFERER, 'http://example.com/'); 
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); 

curl_setopt($curl, CURLOPT_POST, true); 
curl_setopt($curl, CURLOPT_POSTFIELDS, [ 
    'LOGIN' => 'MY_LOGIN', 
    'PASSWORD' => 'MY_PASSWORD', 
    'REMEMBER' => true, 
    'FORMAT' => 'json', 
    'LANG' => 'ru' 
]); 

curl_exec($curl); 
curl_close($curl); 

$curl = curl_init('http://example.com/search/search/?0.42697851033881307'); 
curl_setopt($curl, CURLOPT_HEADER, true); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($curl, CURLOPT_COOKIEJAR, 'cookie.txt'); 
curl_setopt($curl, CURLOPT_COOKIEFILE, 'cookie.txt'); 
curl_setopt($curl, CURLOPT_REFERER, 'http://example.com/'); 
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); 

$postFields = json_encode([ 
    'FORMAT' => 'json', 
    'LANG' => 'ru', 
    'QUERY' => '29061', 
    'QUERY_TYPE' => '2', 
    'QUERY_DATA' => 'S1', 
    'PKW' => 'X', 
    'LKW' => 'X' 
]); 
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST'); 
curl_setopt($curl, CURLOPT_POSTFIELDS, $postFields); 
curl_setopt($curl, CURLOPT_HTTPHEADER, [ 
    'X-Requested-With: XMLHttpRequest', 
]); 

$response = curl_exec($curl); 
curl_close($curl); 
var_dump($response); 

而這裏的響應:

string(1699) "HTTP/1.1 302 Moved Temporarily 
Server: nginx/1.9.2 
Date: Fri, 28 Aug 2015 14:57:20 GMT 
Content-Type: text/html; charset=UTF-8 
Transfer-Encoding: chunked 
Connection: keep-alive 
X-Powered-By: PHP/5.6.10 
Expires: Thu, 19 Nov 1981 08:52:00 GMT 
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0 
Pragma: no-cache 
Set-Cookie: ci_sessions=...; expires=Sat, 27-Aug-2016 14:57:21 GMT; Max-Age=31536000; path=/; httponly 
Location: http://example.com/authorization/setVkorgInfo 

HTTP/1.1 302 Moved Temporarily 
Server: nginx/1.9.2 
Date: Fri, 28 Aug 2015 14:57:21 GMT 
Content-Type: text/html; charset=UTF-8 
Transfer-Encoding: chunked 
Connection: keep-alive 
X-Powered-By: PHP/5.6.10 
Expires: Thu, 19 Nov 1981 08:52:00 GMT 
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0 
Pragma: no-cache 
Set-Cookie: ci_sessions=...; expires=Sat, 27-Aug-2016 14:57:21 GMT; Max-Age=31536000; path=/; httponly 
Set-Cookie: VKORG=5000; expires=Sat, 27-Aug-2016 14:57:21 GMT; Max-Age=31536000; path=/ 
Location: http://example.com/ 

HTTP/1.1 200 OK 
Server: nginx/1.9.2 
Date: Fri, 28 Aug 2015 14:57:21 GMT 
Content-Type: application/json; charset=utf-8 
Transfer-Encoding: chunked 
Connection: keep-alive 
X-Powered-By: PHP/5.6.10 
Expires: Thu, 19 Nov 1981 08:52:00 GMT 
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0 
Pragma: no-cache 
Set-Cookie: ci_sessions=...; expires=Sat, 27-Aug-2016 14:57:22 GMT; Max-Age=31536000; path=/; httponly 

{ 
    "status": true, 
    "arr_messages": [], 
    "data": [] 
}" 

當我通過瀏覽器檢索數據時,我在Chrome的控制檯中看到一個包含「data」:數組中所需數據的對象,但在我的腳本中它是空的。我的要求有什麼問題?

下面是截圖:http://joxi.ru/XEA48NSQozPAbz

如果我錯送我PARAMS得到這個:

{ 
    "status": false, 
    "arr_messages": [ 
     { 
      "id": "123456", 
      "login": "MY_LOGIN", 
      "type": "E", 
      "text": "Error inputs", 
      "date": "2015-08-28 22:24:20" 
     } 
    ], 
    "data": [] 
} 

因此如果我狀態:真實,空「arr_messages」然後我送正確的請求。

UPD:這裏的AJAX功能獲取內容:

var getApiAjax=function(url, req, callback, errorcallback, async) 
{ 
    if(typeof(errorcallback)=="undefined") 
    { 
     errorcalback=function(){return false;} 
    } 
    if(typeof(async)=="undefined") 
    { 
     async=true; 
    } 

    req.FORMAT = "json"; 
    req.LANG = "ru"; 
    var xhr=$.ajax(
    { 
     url: url+"?"+Math.random(), 
     type: "POST", 
     data: req, 
     async: async, 
     dataType: "json", 
     success: function(resp) 
     { 
      var showMes = true; 
      if(spinnerModal.isShowSpinner()){ 
       spinnerModal.hidePleaseWait(); 
       showMes = false; 
      } 

      if(typeof resp.arr_messages != "undefined" && resp.arr_messages.length) { 
       if (resp.status) { 
        showMessage(resp.arr_messages, callback, resp); 
       } else { 
        showMessage(resp.arr_messages, errorcallback, resp); 
       } 

       if(showMes) informModal.showMessage(); 
      } else { 
       if (resp.status) { 
        callback(resp); 
       } else { 
        errorcallback(resp) 
       } 
      }        
     }, 
     error: function(msg) 
     { 
      var arr_messages = new Array(); 

      if(spinnerModal.isShowSpinner()) spinnerModal.hidePleaseWait(); 

      if(typeof msg == "string") 
      { 
       arr_messages.push({ 'text' : msg, 'type' : 'E' }); 
       showMessage(arr_messages, errorcallback, msg, true); 
      } 
      else 
      { 
       errorcallback();    
      } 
     } 
    }); 
    return xhr; 
}; 

我可以調用從瀏覽器的控制檯此功能,並得到我想要的東西

getApiAjax("/search/search/",{ 
      'QUERY'   : "29061", 
      'QUERY_TYPE' : "2", 
      'QUERY_DATA' : "S1", 
      'PKW'   : "X", 
      'LKW'   : "X" 
     }) 

截圖:http://joxi.ru/WL211LSbgN42Xq

所以我無法弄清楚捲毛請求有什麼問題:(

+0

嘗試發送$ postFields通常 - 作爲一個數組。不作爲JSON。如果它是一個真正的HTML表單,那麼數據不會作爲JSON而是作爲html查詢發送。 – uri2x

+0

我以前試過,現在再試一次沒有運氣:( – leealex

回答

0

哇,原來如此出乎意料的簡單:) 我不知道爲什麼,但本網站不檢查cookie的搜索查詢。當一個人打開這個網站時,他只能看到登錄表單,沒有別的,所以如果訪問者是一個人,他必須授權才能訪問這個網站,然後訪問者能夠使用網站的服務,包括搜索。所以我決定授權是我的腳本所必需的。 不眠之夜我想:「如果我不要求擅自將發生什麼」(其實我是看到一些錯誤信息一起工作),所以我扔掉了我的劇本的第一部分後:

$curl = curl_init('http://example.com/authorization/login/'); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($curl, CURLOPT_COOKIEJAR, 'cookie.txt'); 
curl_setopt($curl, CURLOPT_COOKIEFILE, 'cookie.txt'); 
curl_setopt($curl, CURLOPT_REFERER, 'http://example.com/'); 
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); 

curl_setopt($curl, CURLOPT_POST, true); 
curl_setopt($curl, CURLOPT_POSTFIELDS, [ 
    'LOGIN' => 'MY_LOGIN', 
    'PASSWORD' => 'MY_PASSWORD', 
    'REMEMBER' => true, 
    'FORMAT' => 'json', 
    'LANG' => 'ru' 
]); 

curl_exec($curl); 
curl_close($curl); 

和在我的瀏覽器中啓動它。 我看到旁邊嚇了我一跳)))

string(2856) "HTTP/1.1 100 Continue 

HTTP/1.1 200 OK 
Server: nginx/1.9.2 
Date: Sat, 29 Aug 2015 05:38:54 GMT 
Content-Type: application/json; charset=utf-8 
Transfer-Encoding: chunked 
Connection: keep-alive 
X-Powered-By: PHP/5.6.10 
Expires: Thu, 19 Nov 1981 08:52:00 GMT 
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0 
Pragma: no-cache 
Set-Cookie: ci_sessions=.....; expires=Sun, 28-Aug-2016 05:38:55 GMT; Max-Age=31536000; path=/; httponly 

{ 
    "status": true, 
    "arr_messages": [], 
    "data": { 
     "ARTIDINFO": { 
      "203653": { 
       "ARTID": "203653", 
       "PIN": "29061", 
       "BRAND": "...", 
       "NAME": "...", 
       "IMG": "0", 
       "NAMEP": [] 
      }, 
      "301175": { 
       "ARTID": "301175", 
       "PIN": "29061", 
       "BRAND": "...", 
       "NAME": "...", 
       "IMG": "1", 
       "NAMEP": [] 
      }, 
      "1696433": { 
       "ARTID": "1696433", 
       "PIN": "29061", 
       "BRAND": "...", 
       "NAME": "...", 
       "IMG": "1", 
       "NAMEP": [] 
      }..... 

現在我不明白爲什麼服務器發送我的數據,擅自不,當我經過授權O_O