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.

2
  • 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? Commented Sep 22, 2017 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! Commented Sep 22, 2017 at 11:03

1 Answer 1

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.

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

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.