Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/hadoop/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 空cURL json响应_Php_Ajax_Json_Curl - Fatal编程技术网

Php 空cURL json响应

Php 空cURL json响应,php,ajax,json,curl,Php,Ajax,Json,Curl,我试图通过curl从服务器获取数据,但经过授权后,我收到一个空响应。该服务器上的站点使用AJAX显示内容,因此首先我检查了通过浏览器与之交互时发送的标题和查询参数 一般标题: Remote Address:*.*.*.*:80 Request URL:http://example.com/search/search/?0.42697851033881307 Request Method:POST Status Code:200 OK 表格数据: QUERY:29061 QUERY_TYPE:2

我试图通过curl从服务器获取数据,但经过授权后,我收到一个空响应。该服务器上的站点使用AJAX显示内容,因此首先我检查了通过浏览器与之交互时发送的标题和查询参数

一般标题:

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
下面是无休止的谷歌搜索和堆垛溢出的结果:

<?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);
当我通过浏览器检索数据时,我在Chrome的控制台中看到一个对象,在“data”:数组中包含所需的数据,但在我的脚本中它是空的。我的要求怎么了

以下是截图:

如果我发送了错误的参数,我会得到:

{
    "status": false,
    "arr_messages": [
        {
            "id": "123456",
            "login": "MY_LOGIN",
            "type": "E",
            "text": "Error inputs",
            "date": "2015-08-28 22:24:20"
        }
    ],
    "data": []
}
因此,如果我得到状态:true且为空的“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"
        })
截图:


所以我不知道curl请求有什么问题:(

哇,结果出乎意料地简单:) 我不知道为什么,但这个网站不检查cookies的搜索查询。当一个人打开这个网站时,他只能看到登录表单,其他什么都看不到,所以如果访问者是人,他必须授权访问这个网站,然后访问者才能使用包括搜索在内的网站服务。所以我决定我的脚本需要授权。 在失眠之夜之后,我想“如果我未经授权请求,会发生什么情况”(我实际上想看到一些错误消息),所以我扔掉了脚本的第一部分:

$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);
并在我的浏览器中启动了它。 我接下来所看到的使我震惊


现在我不明白为什么服务器在未经授权的情况下向我发送数据,而当我通过授权时,服务器却没有发送数据(哇,结果出乎意料地简单:) 我不知道为什么,但这个网站不检查cookies的搜索查询。当一个人打开这个网站时,他只能看到登录表单,其他什么都看不到,所以如果访问者是人,他必须授权访问这个网站,然后访问者才能使用包括搜索在内的网站服务。所以我决定我的脚本需要授权。 在失眠之夜之后,我想“如果我未经授权请求,会发生什么情况”(我实际上想看到一些错误消息),所以我扔掉了脚本的第一部分:

$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);
并在我的浏览器中启动了它。 我接下来所看到的使我震惊


现在我不明白为什么服务器在未经授权的情况下向我发送数据,而当我通过授权时,服务器不尝试正常发送$postFields(作为数组)。不是作为JSON。如果它是一个真正的HTML表单,那么数据不会以JSON的形式发送,而是以HTML查询的形式发送。我以前尝试过,现在又尝试了一次,但没有成功:(请尝试正常发送$postFields-作为数组,而不是JSON。如果它是真正的HTML表单,则数据不会作为JSON发送,而是作为HTML查询发送。我以前尝试过,现在又尝试过,但没有成功:(
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": []
            }.....