1. #1
    Sencha User
    Join Date
    May 2012
    Posts
    6
    Vote Rating
    0
    muddsg is on a distinguished road

      0  

    Default Answered: How to parse an image link from the xml datasets using php then ‘json_encode’ it.

    Answered: How to parse an image link from the xml datasets using php then ‘json_encode’ it.


    Hi, I have received a xml datasets for a traffic live camera feed.
    In order to access the datasets through a browser,
    need to put the credentials, username and password, which I was given.
    So, how to parse an image link from the xml datasets using php then ‘json_encode’ it.

    the xml datasets


    parsing the xml using php


    parse to sencha javascript store

    Then put the fields with reference to the xml datasets


    Why am I unable to display the picture though I already put it as img src=imageurl in the sencha view.
    Note* I copied the imageurl link and tried to open up with a browser.
    It will download a blank image.

  2. Joking...

    You can package the request with your needed headers and call it from js as you do from php,

    Code:
    Ext.create('Ext.data.Store', {
        model: 'Entry',
        proxy: {
            type: 'ajax',
            headers:{
                "Authorisation": ur_custom_base64_encode(user+':'+passw),
                "UserAgent":'Foobar'
            },
            url : 'zaXMLprovideer/url/',
            reader: {
                type: 'xml',
                rootPorperty: 'feed entry'
            }
        },
    });



    or you can call a php script file that will do the request to "xml datasets" as you do now, and just return the xml response without json_encode-ing.

    zaScript.php
    PHP Code:
    $user='';
    $pass='';
    ...
    $xml= new SimpleXmlElement(....);
    echo 
    $xml->asXML();
    unset(
    $things);
    exit(); 


    Code:
    Ext.create('Ext.data.Store', {
        model: 'Entry',
        proxy: {
            type: 'ajax',
            url : 'zaScript.php',
            reader: {
                type: 'json',
                rootPorperty: 'feed entry'
            }
        },
    });

  3. #2
    Sencha User bluehipy's Avatar
    Join Date
    Mar 2010
    Location
    Romania
    Posts
    606
    Vote Rating
    26
    Answers
    66
    bluehipy will become famous soon enough bluehipy will become famous soon enough

      0  

    Default


    In php you will have to return the result of json_encode to the standard output. In your example you just encode the $xml but you don't print / echo the result.


    So I guess in the end of your file you need to set:


    echo json_encode( $xml );



    On the client, in the store definition, the rootProperty is not the 'entry'. There is some xpath `til the entry, like "feed.entry".


    Also you can let the js read the xml if you want, ST2 has an xml reader

  4. #3
    Sencha User
    Join Date
    May 2012
    Posts
    6
    Vote Rating
    0
    muddsg is on a distinguished road

      0  

    Default


    Hi, yup i saw the xml reader before, but i didnt use it because i dont know how to put the credentials.
    Need the username and password to access the url xml datasets, referring to my 'parsing the xml using php' picture, 'user' and 'pass'.
    I hope you can give an example?
    Sorry.

  5. #4
    Sencha User bluehipy's Avatar
    Join Date
    Mar 2010
    Location
    Romania
    Posts
    606
    Vote Rating
    26
    Answers
    66
    bluehipy will become famous soon enough bluehipy will become famous soon enough

      0  

    Default


    I could but I would totally ruin your learning experience

  6. #5
    Sencha User bluehipy's Avatar
    Join Date
    Mar 2010
    Location
    Romania
    Posts
    606
    Vote Rating
    26
    Answers
    66
    bluehipy will become famous soon enough bluehipy will become famous soon enough

      0  

    Default


    Joking...

    You can package the request with your needed headers and call it from js as you do from php,

    Code:
    Ext.create('Ext.data.Store', {
        model: 'Entry',
        proxy: {
            type: 'ajax',
            headers:{
                "Authorisation": ur_custom_base64_encode(user+':'+passw),
                "UserAgent":'Foobar'
            },
            url : 'zaXMLprovideer/url/',
            reader: {
                type: 'xml',
                rootPorperty: 'feed entry'
            }
        },
    });



    or you can call a php script file that will do the request to "xml datasets" as you do now, and just return the xml response without json_encode-ing.

    zaScript.php
    PHP Code:
    $user='';
    $pass='';
    ...
    $xml= new SimpleXmlElement(....);
    echo 
    $xml->asXML();
    unset(
    $things);
    exit(); 


    Code:
    Ext.create('Ext.data.Store', {
        model: 'Entry',
        proxy: {
            type: 'ajax',
            url : 'zaScript.php',
            reader: {
                type: 'json',
                rootPorperty: 'feed entry'
            }
        },
    });

  7. #6
    Sencha User bluehipy's Avatar
    Join Date
    Mar 2010
    Location
    Romania
    Posts
    606
    Vote Rating
    26
    Answers
    66
    bluehipy will become famous soon enough bluehipy will become famous soon enough

      0  

    Default


    Well, as you see I used there "ur_custom_base64_function" meaning there is no native js base64 encoding, (except mozilla) so you have to define the encoding function, there are a couple on internet.

    Regarding the second approach, your model name is wrong in my opinion... "new" is not a good model name... model names shoud not be reserved words and should be uppercase. (but I don t think it is necesarily the issue in your case, fix it anyway)

    The entry nodes have: id, title, summary, author, content, category... so there "title" and "updated" which you are specifying in model, that s fine.


    On the view, again, the "new" naming thing is not inspired... change it maybe.

    Also the code excerpt is not relevant to the cause, you should expose the listing part. and the store part.

    Also check if php service is returning a valid xml as you expect.

  8. #7
    Sencha User
    Join Date
    May 2012
    Posts
    6
    Vote Rating
    0
    muddsg is on a distinguished road

      0  

    Default


    Ouh ok. I will change the name.

    Another thing, i tried to return the xml response without json_encode-ing in the php using your example and in the console it will say 'You're trying to decode an invalid JSON String'

  9. #8
    Sencha User bluehipy's Avatar
    Join Date
    Mar 2010
    Location
    Romania
    Posts
    606
    Vote Rating
    26
    Answers
    66
    bluehipy will become famous soon enough bluehipy will become famous soon enough

      0  

    Default


    Maybe you kept the reader type to 'json' ?

  10. #9
    Sencha User
    Join Date
    May 2012
    Posts
    6
    Vote Rating
    0
    muddsg is on a distinguished road

      0  

    Default


    Sorry for the late reply. I solve it already.

    i forgot to put this in the php.
    header('Content-type: application/xml');

    before the
    echo $xml->asXML();

Thread Participants: 1

Tags for this Thread