Jump to content

Two near identical scripts. One works, one doesn't


tunnelboy

Recommended Posts

Trying to create a very simple API script sending XML data. When I send the "hard coded" XML, it works perfectly. When I add a form to supply the data for the XML, I get a 500 Internal Server error. Have tried it on two different servers. No error in the logs. Stumped.

Examples below are VERY simplified (and obviously won't do anything as-is) to show what I'm dealing with.

This one works fine:

<?php
$xml = "<Order><UserId>foo</UserId><Password>foo</Password><Mode>Test</Mode><Name>Charles R. Hodges</Name>";
$xml .= "</Order>"; 

$url = 'http://sitename.com';
$ch = curl_init($url);
 
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);
curl_close($ch);
$xml = simplexml_load_string($response);
print_r($xml);
?>

And then I use this one and all heck breaks loose with the 500 Internal Error:

<?php
if (isset($_POST['submit'])) {
$xml = "<Order><UserId>foo</UserId><Password>foo</Password><Mode>Test</Mode><Name>{$_POST['name']}</Name>";
$xml .= "</Order>"; 

$url = 'http://sitename.com';
$ch = curl_init($url);
 
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);
curl_close($ch);
$xml = simplexml_load_string($response);
print_r($xml);
exit;
}
?>

<html>... etc
<form method="post" action="">
Name: <input type="text" name="name" />
<p><input type="submit" name="submit" "Add to XML string and send" />
</form>
?>

 

Link to comment
Share on other sites

A 500 means there's something wrong with your script. Could be a syntax error. But it's really hard to spot syntax errors when you only post "very simplified" code.

Do you know how to configure php.ini settings on your server(s)? Change display_errors to "on" and error_reporting to -1. Then restart your web server and/or PHP and try running your script again. There should be error messages this time.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.