[VERY NEED HELP]Problems with Json Decoding

Discussion in 'PHP' started by angeal, Jun 7, 2010.

  1. #1
    hello all :)

    i was build web application for get related suggest data from yahoo..

    data sources from yahoo extension is json.. I decode it with PHP

    but the problem is my script just obtain first character from each result data..

    ex :

    json output appears "maddona","donna","knight"

    my script ouput is m,d,k

    i don`t know how to fix it...

    this is my script

    <?php
    
    
    define('YAHOO_API_KEY', 'ABQIAAAA9B5AsbW0tMeo9K_FAr5zmBRfWlH0hUzD7VnCL8diZlXkna8XOxRMWVPhFXxBoDZbMViK2JQnXa_SHw');
    
    function digital_curl_get($url, $params)
    {
    	$post_params = array();
    	foreach ($params as $key => &$val) {
    	  if (is_array($val)) $val = implode(',', $val);
    		$post_params[] = $key.'='.urlencode($val);
    	}
    	$post_string = implode('&', $post_params);
    
    	$fullurl = $url."?".$post_string;
    
    	$ch = curl_init();
    	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    	curl_setopt($ch, CURLOPT_URL, $fullurl);
    	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    	curl_setopt($ch, CURLOPT_USERAGENT, 'Mailana (curl)');
    	$result = curl_exec($ch);
    	curl_close($ch);
    
    	return $result;
    }
    
    function perform_boss_web_search($termstring)
    {
    	$searchurl = 'http://search.yahooapis.com/WebSearchService/V1/relatedSuggestion';
    	$searchparams = array(
    		'appid' => 'YahooDemo',
    		'results' => '10', 
    		'query' => 'pdf',
    		'output' => 'json',
    		);
    	$response = digital_curl_get($searchurl, $searchparams);
    	
    	$responseobject = json_decode($response, true);
    	error_log(print_r($responseobject, true));
    	
    	if ($responseobject['ResultSet']['Result']==0)
    		return array();
    	
    	$allresponseresults = $responseobject['ResultSet']['Result'];
    
    	$result = array();
    	foreach ($allresponseresults as $responseresult)
    	{
    		$result[] = array(
    			'ResultSet' => $responseresult['ResultSet'],
    		);
    	}
    
    	return $result;
    }
    
    if (isset($_REQUEST['q'])) {
    	$termstring = urldecode($_REQUEST['q']);
    } else {
    	$termstring = '';
    }
    ?>
    <html>
    <head>
    <title>Test page for Google, BOSS and Bing search apis</title>
    </head>
    <body>
    <div style="padding:20px;">
    <center>
    <form method="GET" action="yahoo.php">
    Search terms: <input type="text" size="40" name="q" value='<?=$termstring?>'/>
    </form>
    </center>
    </div>
    <?php
    if ($termstring!='') {
    
    	$bossresults = perform_boss_web_search($termstring);
    
    	print '<br/><br/><h2>BOSS search results ('.count($bossresults).')</h2><br/>';
    	foreach ($bossresults as $result) {
    		print '<span style="font-size:80%">'.$result['ResultSet'].'</span><br/><hr/>';
    	}
    
    }
    
    ?>
    PHP:
    anybody.. please help me :(
     
    angeal, Jun 7, 2010 IP
  2. lukeg32

    lukeg32 Peon

    Messages:
    645
    Likes Received:
    19
    Best Answers:
    1
    Trophy Points:
    0
    #2
    Its your array processing in the perform_boss_web_search function.

        foreach ($allresponseresults as $responseresult)
        {
            $result[]= $responseresult;
        }
    PHP:
    And when you come to output it;

     print '<br/><br/><h2>BOSS search results ('.count($bossresults).')</h2><br/>';
        foreach ($bossresults as $result) {
            print '<span style="font-size:80%">'.$result.'</span><br/><hr/>';
        }
    PHP:
     
    lukeg32, Jun 7, 2010 IP
  3. angeal

    angeal Guest

    Messages:
    237
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    28
    #3
    wow.. thanks for the correction dude :)

    user reputation added :)
     
    angeal, Jun 8, 2010 IP