• Resolved Dongsan

    (@dongsan)


    Hi,

    I find this is a great map plugin, and will purchase this after I’m done setting things up completely in my localost environment.

    So far everything was good until I was trying to search for a marker with its markername and update its geocode. To do this, I need to search by markername and get its ID first. Once I get the ID of the marker, then I can update the marker. I can search for a marker, but the response is an array from which I need to parse the marker information such as ID.

    Here’s how I search for a marker.

    $map_api_url = plugins_url()."/leaflet-maps-marker-pro/leaflet-api.php";
    						$user_id = $current_user->id;
    						$user_info = get_userdata( $user_id );
    						$company = $user_info->company_name;
    						$office_address = $user_info->office_address;
    
    						echo "company = ".$company."<br/>";
    
    						$jsonp = wp_remote_post( $map_api_url, array(
    									'method'		=> 'POST',
    									'body'			=> array(
    										'key'			=> '0406',
    										'signature'		=> 'JMmfJgmJgNWhWI%2B1915QOGfP4Cw%3D',
    										'expires'		=> '1398743380',
    										'action'		=> 'search',
    										'type'			=> 'marker',
    										'searchkey'		=> 'markername',
    										'searchvalue'	=> $company
    								)
    							)
    						);
    
    						if( is_wp_error( $jsonp ) ) {
    							$error_message = $jsonp->get_error_message();
    							echo "Something went wrong: $error_message";
    						} else {
    
    								// Yes, found the marker with the given markername. Now, need to figure out its ID.
    
    						}

    However, what I get as a response is as below, exactly what is described in https://www.mapsmarker.com/docs/api-tutorials/web-api/#response.

    Array
    (
        [headers] => Array
            (
                [date] => Mon, 28 Apr 2014 11:41:09 GMT
                [server] => Apache
    	    .......
                [content-type] => application/json; charset=utf-8
            )
    
        [body] => jsonp({
    	"success":true,
    	"searchkey":"markername",
    	"searchvalue":"dongsan",
    	"searchresults":1,
    	"message":"Search completed - showing 1 results below",
    	"data": [
    		{"id":"68",
    		"markername":"dongsan",
    		"basemap":"bingroad",
    		"layer":"1",
    		"lat":"39.904030",
    		"lon":"116.407526",
    		"icon":"",
    		"popuptext":"",
    		"zoom":"11",
    		"openpopup":"1",
    		"mapwidth":"640",
    		.......
    		"wms10":"0",
    		"kml_timestamp":"",
    		"address":"",
    		"gpx_url":"",
    		"gpx_panel":"0"
    		}]});
        [response] => Array
            (
                [code] => 200
                [message] => OK
            )
    
        [cookies] => Array
            (
                [0] => WP_Http_Cookie Object
                    (
                        [name] => qtrans_cookie_test
                        [value] => qTranslate Cookie Test
                        [expires] =>
                        [path] => /wordpress/
                        [domain] => 127.0.0.1
                    )
    
            )
    
        [filename] =>
    )

    How do I parse this ID from the above array(?) ? I tried json_decode and failed.

    Thank you.

    https://www.ads-software.com/plugins/leaflet-maps-marker/

Viewing 6 replies - 1 through 6 (of 6 total)
  • Hi Dongsan,

    please use the following function to extract the JSONP resultset with PHP:

    function jsonp_decode($jsonp, $assoc = false) {
        if($jsonp[0] !== '[' && $jsonp[0] !== '{') {
           $jsonp = substr($jsonp, strpos($jsonp, '('));
        }
        return json_decode(trim($jsonp,'();'), $assoc);
    }

    usage:
    $data = jsonp_decode($jsonp);

    best,

    Robert

    Thread Starter Dongsan

    (@dongsan)

    Thank you Robert for the reply. The thing is, I’ve already found that code in google and tried. Printing $data (echo $data;) yields null, and nothing is printed by $data->id. Really at a loss what I have done wrong…

    the result is an array, so it can be displayed by print_r($data) respectively accessed by echo $data[‘identifer’]

    Thread Starter Dongsan

    (@dongsan)

    Thank you and sorry for bugging you with this simple problem, but I’m not a programmer nor in a tech-side. Please understand.

    I tried again with the codes you provided, but still no fruits.

    print_r($data) prints nothing, and echo $data[‘id’] prints nothing, either.

    Giving it a second glance, I guess this is because the response I get from the query is a bit different from what is described in https://www.mapsmarker.com/docs/api-tutorials/web-api/#response.

    Under https://www.mapsmarker.com/docs/api-tutorials/web-api/#response, it is described that the response is as below;

    jsonp(
      {
        success: true,
        searchkey: "markername",
        ....
        - data: [
          - {
              id: "1",
              markername: "test2",
              ....
            },
         + { ... },
         ....
       ]
      }
    )

    However, the response I actually get is as below;

    Array
    (
        [headers] => Array
            (
                [date] => Mon, 28 Apr 2014 11:41:09 GMT
                [server] => Apache
    	    .......
                [content-type] => application/json; charset=utf-8
            )
    
        [body] => jsonp({
    	"success":true,
    	"searchkey":"markername",
    	"searchvalue":"dongsan",
    	"searchresults":1,
    	"message":"Search completed - showing 1 results below",
    	"data": [
    		{"id":"68",
    		"markername":"dongsan",
    		.......
    		}]});
        [response] => Array
            (
                [code] => 200
                [message] => OK
            )
    
        [cookies] => Array
            (
                [0] => WP_Http_Cookie Object
                    (
                        [name] => qtrans_cookie_test
                        [value] => qTranslate Cookie Test
                        [expires] =>
                        [path] => /wordpress/
                        [domain] => 127.0.0.1
                    )
    
            )
    
        [filename] =>
    )

    So, I get not only jsonp({...}) but also get

    Array(
    [headers]=>Array()
    [body]=>jsonp()
    [response]=>Array()
    [cookies]=>Array()
    [filename]=>
    )

    Could this be the reason of the problem?

    Thread Starter Dongsan

    (@dongsan)

    Not sure how I did it, and why it is working, but I managed to get it working. The biggest fault I made was that I used wp_remote_post() to retrieve the result set. I changed the codes as below, and now it’s working. Just in case there are other silly beginners like myself, here’s the code I have:

    function jsonp_decode($jsonp, $assoc = false) {
    if($jsonp[0] !== '[' && $jsonp[0] !== '{') {
       $jsonp = substr($jsonp, strpos($jsonp, '('));
    }
    $jsonp = json_decode(trim($jsonp,'();'), $assoc);
    $jsonp = $jsonp->data[0];
    return $jsonp;
    }
    
    $jsonp = $response['body'];
    $data = jsonp_decode( $jsonp );
    
    echo "ID = ".$data->id;
    echo "Markername = ".$data->markername;
    echo "layer = ".$data->layer;

    great to hear that you could solve this issue!
    best,
    Robert

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘jsonp’ is closed to new replies.