skip to Main Content

I am using plesk api to return informaton from plesk.
It gets put into an xml string eg

$response = $client->request($request);

The string has this information in

<database>
<get-db>
<result>
<filter-id>domain name</filter-id>
<id>34</id>
<name>database</name>
<type>mysql</type>
...etc snip
</result>
<result>
<filter-id>domain name</filter-id>
<id>36</id>
<name>database</name>
<type>mysql</type>
...etc snip
</result>
</get-db>
</database>

What I want to put the result into a 2 dimensional array.

I want the first to be name and I also need the id

I have tried using preg_match to get the tags, but for some reason I am only getting the first tag. And of course the function isn’t putting it inside a 2 dimensional array yet.

function tags($string, $tagname)
{
    $pattern = "#<s*?$tagnameb[^>]*>(.*?)</$tagnameb[^>]*>#s";
    preg_match($pattern, $string, $matches);
    return $matches;
}

This is so I can match the name and get the id you see.

I am editing because I have just found something that might help, but I haven’t worked it out yet

$xml=simplexml_load_string($response) or die("Error: Cannot create object");

I think this is for parsing xml, but can’t seem to get it parse my xml package properly.

Also tried this

$data = simplexml_load_string($response);
echo $data->result[0]->name;

But this doesn’t seem to work.

2

Answers


  1. Chosen as BEST ANSWER

    I have solved this now

    $response = $client->request($request); // Send query to Plesk host
    echo $response; // show response
    
    $xml = simplexml_load_string($response);
    
    
    echo $xml->database->{'get-db'}->result[0]->name;
    // This gets the first tag called name
    
    //This loops through and gets every tag called name
    foreach ($xml->database->{'get-db'}->result as $result)
    {
       echo '<pre>'.$result->name.'</pre>';
    //If I want to now I can put this result into an array here, but I find I do not need to now. As I only want to find the id of a matched database. So no array needed now, as I can use this loop
    }
    

  2. Using Symfony Serializer Component, I created a method in API client that encode an array to xml, send to Plesk and decode response as array.

    /**
     * Encode array to XML, perform API request and
     * decode response to array.
     *
     * @param  array $data
     * @return array
     */
    public function encodeAndRequest($data)
    {
        $encoder = new XmlEncoder('packet');
        return $encoder->decode($this->request($encoder->encode($data, 'xml')), 'xml');
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search