FindPersons - SOAP POST using PHP

Not Yet Reviewed

How can i display in php the firstName, lastName and targetName from a SOAP Response?

Here's my simple php script

<?php

$curl = curl_init();

curl_setopt_array($curl, array(
CURLOPT_URL => "https://company.hosted.xmatters.com/api/services/xmatters-5.5.47.xmatters-5.5.47HttpSoap12Endpoint/",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:sch=\"http://www.xmatters.com/webservices/schema#5.5.47\">\r\n soapenv:Header/\r\n soapenv:Body\r\n sch:FindPersons\r\n sch:userws-soaptesting/sch:user\r\n sch:passwordsoaptesting/sch:password\r\n sch:clientTimestamp/\r\n sch:clientIP/\r\n sch:clientOSUser/\r\n sch:company/\r\n sch:searchParameters\r\n sch:targetName/sch:targetName\r\n /sch:searchParameters\r\n /sch:FindPersons\r\n /soapenv:Body\r\n/soapenv:Envelope",
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache",
"content-type: text/plain",
),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}

0

Comments

7 comments
Date Votes

Please sign in to leave a comment.

  • Aris - my fellow xPert Travis is working on this. Stay tuned.

    0
  • Hey Aris!
    PHP isn't a strong point for me, but I'm looking at your SOAP request and it seems to be missing all of the ">" and most of the "<" that might have been a result of pasting or copying? Not sure. Also, make sure you include the company name, which can be found in the upper right corner when you are logged into the xMatters UI, just under your name.
    This payload worked for me:
    ```

    <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:sch="http://www.xmatters.com/webservices/schema#5.5.47">
    soap:Header/
    soap:Body
    sch:FindPersons
    sch:usersoapuserhere/sch:user
    sch:passwordpasswodhere/sch:password
    sch:clientTimestamp/sch:clientTimestamp
    sch:clientIP/sch:clientIP
    sch:clientOSUser/sch:clientOSUser
    sch:companyCompany Name/sch:company
    sch:searchParameters
    sch:roleName/sch:roleName
    sch:targetNametdepuy/sch:targetName
    /sch:searchParameters
    /sch:FindPersons
    /soap:Body
    /soap:Envelope
    ```

    Oh, and also check that content-type header. I think it should be "application/soap+xml", not "text/plain".
    Give those a shot and let me know how it goes. I can probably dig out a php interpreter from somewhere around here if needed.

    Happy Thursday!
    --- Travis

    0
  • Huh, yea, zendesk stripped out the tags on my post. too.

    0
  • <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:sch="http://www.xmatters.com/webservices/schema#5.5.47">
       <soap:Header/>
       <soap:Body>
          <sch:FindPersons>
             <sch:user>soapuserhere</sch:user>
             <sch:password>passwodhere</sch:password>
             <sch:clientTimestamp></sch:clientTimestamp>
             <sch:clientIP></sch:clientIP>
             <sch:clientOSUser></sch:clientOSUser>
             <sch:company>Company Name</sch:company>
             <sch:searchParameters>
                <sch:roleName></sch:roleName>
                <sch:targetName>tdepuy</sch:targetName>
             </sch:searchParameters>
          </sch:FindPersons>
       </soap:Body>
    </soap:Envelope>
    
    0
  • Yeah, when I pasted it, it removed the < and > from CURLOPT_POSTFIELDS.
    It didn't asked for the Company.

    If I change it to "content-type: application/json+xml" it gives me the error
    "http://www.w3.org/2005/08/addressing/soap/faultsoapenv:VersionMismatchTransport level information does not match with SOAP Message namespace URI"

    if I change it to "content-type: application/xml" it gives me the error
    "The endpoint reference (EPR) for the Operation not found is /api/services/xmatters-5.5.47.xmatters-5.5.47HttpSoap12Endpoint/ and the WSA Action = null. If this EPR was previously reachable, please contact the server administrator."

    I replaced my php script since it's not working with getElementsByTagName.
    This is my new php script, it's working, it's displaying targetName which is what I defined but it only displayed 1 Result. I need to have it display all.

    <?php
    $data = "soapenv:Envelope...../soapenv:Envelope";

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $endpoint);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: text/xml; charset=utf-8", "Content-Length: " . strlen($data)));
    $output = curl_exec($ch);
    curl_close($ch);

    $response = DOMDocument::loadXml($output);
    $value = $response->getElementsByTagName('targetName')->item(0)->textContent;
    echo $value;

    0
  •  

    Here's the complete php script.

     

    <?php

    $endpoint = "https://instance.xmatters.com/api/services/xmatters-5.5.47.xmatters-5.5.47HttpSoap12Endpoint/";

    $request = "<soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\"
    xmlns:sch=\"http://www.xmatters.com/webservices/schema#5.5.47\">\r\n
    <soap:Header/>\r\n
    <soap:Body>\r\n
    <sch:FindPersons>\r\n
    <sch:user>username</sch:user>\r\n
    <sch:password>password</sch:password>\r\n
    <sch:clientTimestamp></sch:clientTimestamp>\r\n
    <sch:clientIP></sch:clientIP>\r\n
    <sch:clientOSUser></sch:clientOSUser>\r\n
    <sch:company></sch:company>\r\n
    <sch:searchParameters>\r\n
    <sch:roleName></sch:roleName>\r\n
    <sch:targetName></sch:targetName>\r\n
    </sch:searchParameters>\r\n
    </sch:FindPersons>\r\n
    </soap:Body>\r\n</soap:Envelope>";

    $curl = curl_init();
    curl_setopt_array($curl, array(
    CURLOPT_URL => $endpoint,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => "",
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => "POST",
    CURLOPT_POSTFIELDS => $request,
    CURLOPT_HTTPHEADER => array(
    "cache-control: no-cache",
    "content-type: text/plain"
    ),
    ));

    $response = curl_exec($curl);

    curl_close($curl);

    $dom = new DomDocument();
    $dom -> loadXML($response);
    $x = 0;

    echo "UserID -- FirstName LastName \r\n";

    while(is_object($targetName = $dom->getElementsByTagName('targetName')->item($x)))
    {

    $targetName = $dom->getElementsByTagName('targetName')->item($x)->textContent;
    $firstName = $dom->getElementsByTagName('firstName')->item($x)->textContent;
    $lastName = $dom->getElementsByTagName('lastName')->item($x)->textContent;

    echo "[" . strtolower($targetName) . "] -- " . $firstName . " " . $lastName . "\r\n";

    $x++;

    }
    0
  • It is markdown, so enclose the code in three backtics `. But you have to make sure there is not a blank line after the backtics. It is rather sensitive.
    Interesting on the content type. I'm not sure why the confusion on that. Good to hear you got it working.

    0

Didn't find what you were looking for?

New post