dqz7636 2012-01-14 15:24
浏览 38

无法通过JSON传递数据并在PHP中使用CURL获取JSON

This is a piece of code of my program. Actually, I want to use json_encode($arr) to pass the result from if block to the "else" block by using CURL to get the json data.

if( $_SERVER["SERVER_ADDR"]=='xxxx' and $_GET["content"]==1 ){
    // do search here and get the result and total no. of result
    list($totalRecord, $result) = $api->loadResult($keyword);

    $categoryArray = $category->setInCategoryArray($result['ResultSet'], 'taobao', $categoryArray, $page, $isCategory, false);
    $arr = array($totalRecord, $categoryArray) ;
    .....

    echo json_encode($arr); //<~~~print the json for CURL to get this data

    exit() ;

}else{  // run this part first          
    $header[0] = "Accept: text/xml,application/xml,application/xhtml+xml,"; 
    $header[0] .= "text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"; 
    $header[] = "Cache-Control: max-age=0"; 
    $header[] = "Connection: keep-alive"; 
    $header[] = "Keep-Alive: 300"; 
    $header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7"; 
    $header[] = "Accept-Language: en-us,en;q=0.5"; 
    $header[] = "Pragma: "; // browsers keep this blank. 

    // this url will call this php file again and enter above if-statement
    $url = "http://xxx/?keyword=".$_GET["keyword"]."&content=1&page=".$_GET["page"];   
    $curl = curl_init();

    curl_setopt($curl, CURLOPT_URL, $url); 
    if( $postvalue!='' ){
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $postvalue);
    }
    curl_setopt($curl, CURLOPT_USERAGENT, 'Googlebot/2.1 (+http://www.google.com/bot.html)'); 
    curl_setopt($curl, CURLOPT_HTTPHEADER, $header); 
    curl_setopt($curl, CURLOPT_REFERER, 'http://www.google.com'); 
    curl_setopt($curl, CURLOPT_ENCODING, 'gzip,deflate'); 
    curl_setopt($curl, CURLOPT_AUTOREFERER, true); 
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($curl, CURLOPT_TIMEOUT, 3000);

    $html = curl_exec($curl); //<~~ problem here
    curl_close($curl); // close the connection 

    $arr = json_decode($html); //<~~~ lost data

}

The order is: use CURL in else-block -> get the result from if-block -> after getting the result from if-block, decode it in else-block.

If I directly print the result of json_encode($arr), the result is the following json which is okay:

["14781",{"result":[{"auctionId":"12362201891","title":"\u4e30\u7530\u96f7\u514b\u8428\u65af \u9646\u5730<\/span>\u5de1<\/span>\u6d0b<\/span>\u8230<\/span> \u5927\u9738\u738b\u4e13\u7528\u5927\u5305\u56f4\u811a\u57ab A\u6807","shopName":"\u946b\u70ab\u8f66\u54c1\u4e13\u8425\u5e97","itemImage":"http:\/\/img02.taobaocdn.com\/bao\/uploaded\/i2\/T1zDejXfloXXaYlqna_090124.jpg_100x100.jpg","currentPrice":205,"bidTimes":null,"endBidDate":{"second":56,"minute":44,"hour":12,"day":1,"month":0,"year":0},"buyPrice":205,"estimatePrice":"22.96"},

However, I cannot get the data from the following statement:

$html = curl_exec($curl); //<~~ problem here
curl_close($curl); // close the connection 

$arr = json_decode($html); //<~~~ cannot get data.

After I decode the json. It returns the structure of the array, but all the content are null value. The following json shows the result after using CURL.

[null,{"result":[{"auctionId":null,"title":null,"shopName":null,"itemImage":null,"currentPrice":0,"bidTimes":null,"endBidDate":{"second":57,"minute":50,"hour":14,"day":13,"month":0,"year":42},"buyPrice":"0.00"}]},"O:19:\"KeywordManipulation\":8:{s:11:\"totalRecord\";N;s:6:\"result\";N;s:14:\"conbinedRecord\";N;s:12:\"\u0000*\u0000lang_from\";s:2:\"en\";s:10:\"\u0000*\u0000lang_to\";N;s:16:\"\u0000*\u0000language_list\";a:25:{s:2:\"ar\";s:6:\"Arabic\";s:2:\"bg\";s:9:\"Bulgarian\";s:5:\"zh-

What is the problem of this program? It just want to pass the result from if-block to else-block by using CURL.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 matlab(相关搜索:紧聚焦)
    • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
    • ¥15 路易威登官网 里边的参数逆向
    • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
    • ¥50 需求一个up主付费课程
    • ¥20 模型在y分布之外的数据上预测能力不好如何解决
    • ¥15 processing提取音乐节奏
    • ¥15 gg加速器加速游戏时,提示不是x86架构
    • ¥15 python按要求编写程序
    • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入