0

I've been trying all morning to get an XML string uploaded via an API so that it submits my order but no matter what I try it simply isn't working for me.

My URL:

$url = "http://example.com/SubmitOrder?apiKey=ABC123&clientID=MYId&orderXml=".$xml;

$xml is my xml details already pre-formatted.

I then put this into my curl section:

$curl = curl_init();
curl_setopt_array($curl, array(
  CURLOPT_RETURNTRANSFER => 1,
  CURLOPT_URL            => $url
));
$resp = curl_exec($curl);

$xml1=simplexml_load_string($resp) or die("Error: Cannot create object");

print_r($xml1);
echo "Submitted";

The response I get is "Error: Cannot create object" and I can see that my details have not been submitted.

Where am iI going wrong ??

Many thanks.

Kevin Wenger
  • 1,290
  • 1
  • 10
  • 28
Wilkesy
  • 15
  • 5
  • did you consider adding a temporary simple debug line to see what is actually coming back from your request? `var_dump($resp);` after your `curl_exec()` call? – Wee Zel Sep 22 '17 at 10:09
  • Yes i had this in there but was coming back blank. Exlored this further and it was down to the encoding of the URL and the API key. There was a special character in the API key that was not encoding properly when sent therefore not authenticating. Simple things!!! Appreciate all your help everyone! – Wilkesy Sep 22 '17 at 11:03

1 Answers1

0

You can check your response data and create data in this code example.

<?php
$url = 'https://www.w3schools.com/xml/note.xml';

$resp = file_get_contents($url);

if (resp) {
    $string = simplexml_load_string($resp);
    var_dump($movies);
}

but if you want get data in method curl try this.

<?php

$html_brand = 'https://www.w3schools.com/xml/note.xml';
$ch = curl_init();

$options = array(
    CURLOPT_URL            => $html_brand,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HEADER         => true,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_ENCODING       => "",
    CURLOPT_AUTOREFERER    => true,
    CURLOPT_CONNECTTIMEOUT => 120,
    CURLOPT_TIMEOUT        => 120,
    CURLOPT_MAXREDIRS      => 10,
);
curl_setopt_array( $ch, $options );
$response = curl_exec($ch);
switch ($http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE)) {
    case 200:  # OK
        break;
    default:
        echo 'Status code HTTP: ', $http_code, "\n";
}

var_dump($response);
curl_close($ch);
die();

Check your data. And check your response status code.