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.