1

How would I submit this test.xml file in PHP using this CURL command:

 curl -X POST -k -d @test.xml -H "Content-Type: application/xml" --user username:password https://www.url.com

This isn't working for me:

 $ch = curl_init();

 //Get the response from cURL
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

 //Set the Url
 curl_setopt($ch, CURLOPT_URL, 'https://username:[email protected]');

 //Create a POST array with the file in it
 $postData = array(
'testData' => '@test.xml',
 );
 curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);

 // Execute the request
 $response = curl_exec($ch);

I'm also wondering how I could submit XML written in the code instead of an XML file.

2
  • 1
    dont put username\password in the url Commented Nov 10, 2016 at 20:52
  • is that the only thing wrong in my code? and how would I pass the username and password Commented Nov 10, 2016 at 20:53

1 Answer 1

1

You should not put username and password in the url. You can use curl_setopt($p, CURLOPT_USERPWD, $username . ":" . $password); for $username and $password. Eg below:

$p = curl_init($url);
curl_setopt($p, CURLOPT_HTTPHEADER, array('Content-Type: application/xml'));
curl_setopt($p, CURLOPT_HEADER, 1);
curl_setopt($p, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($p, CURLOPT_POSTFIELDS, $postData);
curl_setopt($p, CURLOPT_POST, 1);
curl_setopt($p, CURLOPT_RETURNTRANSFER, TRUE);
$return = curl_exec($p);
curl_close($p);
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.