2

I'm hoping someone can help me out.

I have an API call using this URL.

https://192.168.11.234:4444/webconsole/APIController?reqxml=<Request><Login<Username>XXXXX</Username><Password>XXXXX></Password></Login><Get><User><Username>manager</Username></User></Get></Request>

This works fine and returns an XML as expected.

However I'm attempting to do the same thing with PHP and cURL.

I have very little experience with cURL

So far I've tried variations of this but I get no response. only a blank page. wondering if maybe someone could help me out.

<?php
$input_xml = '<Request><Login><Username>XXXX</Username><Password>XXXX</Password></Login><Get><User></User></Get></Request>';
$url = "https://192.168.11.234:4444/webconsole/APIController";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);

curl_setopt($ch, CURLOPT_POSTFIELDS,
            "reqxml=" . $input_xml);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 300);
$data = curl_exec($ch);
curl_close($ch);

$array_data = json_decode(json_encode(simplexml_load_string($data)), true);

print_r('<pre>');
print_r($array_data);
print_r('</pre>');
?>

I will admit I have not worked with cURL much, so I may be way off.

4 Answers 4

1

I love you guys(or gals) This works:

<?php
$input_xml = '<Request><Login><Username>XXXXX</Username><Password>XXXXXX</Password></Login><Get><User></User></Get></Request>';
$url = "https://192.168.11.234:4444/webconsole/APIController";
$ch = curl_init();
$headers = [ 
    "Content-type: text/xml;charset=\"utf-8\"", 
    "Accept: text/xml", 
    "Content-length: ".strlen($xml_data)
]; 

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

        curl_setopt($ch, CURLOPT_URL, $url."?reqxml=" . urlencode($input_xml));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 300);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
           if (curl_error($ch)) {
           echo 'error:' . curl_error($ch);
            }
        $data = curl_exec($ch);
        var_dump($data);
        curl_close($ch);


        $array_data = json_decode(json_encode(simplexml_load_string($data)), true);

        print_r('<pre>');
        print_r($array_data);
        print_r('</pre>');
?>

Thought I was close, setting the correct headers, and changing:

curl_setopt($ch, CURLOPT_URL, $url);

To:

curl_setopt($ch, CURLOPT_URL, $url."?reqxml=" . urlencode($input_xml));

Is what helped. Thanks much for the help!

Sign up to request clarification or add additional context in comments.

2 Comments

Turning off CURLOPT_SSL_VERIFYPEER allows man in the middle (MITM) attacks. If your PHP installation doesn't have an up-to-date CA root certificate bundle, download the one at the curl website and save it on your server: curl.haxx.se/docs/caextract.html Then set a path to it in your php.ini file, e.g. on Windows: curl.cainfo=c:\php\cacert.pem (source)
Whose answer helped you the most, @user3294740? Please read this article "What should I do when someone answers my question?"
0

What do you get when you try the following:

  • var_dump($data);
  • curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/xml'));

Edit: Oh, and CURLOPT_POST = true

Comments

0

Remove the POSTFIEILDS line and replace the URL line with this

curl_setopt($ch, CURLOPT_URL, $url."?reqxml=" . urlencode($input_xml));

That should work.

Comments

0

1) If your server doesn't accept POST requests then remove this line:

curl_setopt($ch, CURLOPT_POSTFIELDS, "reqxml=" . $input_xml);

and add:

curl_setopt($ch, CURLOPT_URL, $url.'?reqxml='.urlencode($input_xml));

to send data in GET request.

2) Set the correct HTTP headers:

$headers = [ 
    "Content-type: text/xml;charset=\"utf-8\"", 
    "Accept: text/xml", 
    "Content-length: ".strlen($xml_data)
]; 

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

3) To see possible errors, add these lines before line $data = curl_exec($ch);:

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
if (curl_error($ch)) {
    echo 'error:' . curl_error($ch);
}

4) Also you need to add check for simplexml_load_string, because it returns false on failure:

if (simplexml_load_string($data) === false) {
// do something
}

Please let me know if my answer helped you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.