I am using Mink with the Goutte webdriver trying to replace the contents of a form in a website with an XML feed.
I coded the following method:
public function replaceField($field)
{
$baseText = '<?xml version="1.0" encoding="UTF-8"?>
<RiskAssessmentReply xmlns="http://test.com" >
<!-- ExternalId of the Order -->
<OrderId>TO_REPLACE</OrderId>
<RiskInfo>
<Actions>
<SystemAction>SystemAction</SystemAction>
<FinalAction>FinalAction</FinalAction>
</Actions>
<Score SystemScore="0"/>
</RiskInfo>
<!-- One of Accept, Manual_Accept, Reject, Cancel, or Ignore -->
<ResponseCode>Accept</ResponseCode>
<StoreId>TESTSTORE</StoreId>
</RiskAssessmentReply>';
$textWithOrderId = preg_replace('/TO_REPLACE/', $GLOBALS['ORDER_ID'], $baseText);
$this->getSession()->getPage()->fillField($field, $textWithOrderId);
}
Which basically contains the XML feed, then I replace a part of it with an order ID that I have from beforehand and call the function fillField which comes bundled with Mink.
The problem is that it does not just paste the text that I provide, but formats it in a weird manner by setting backslashes before the " symbols, like this:
<?xml version=\"1.0\" encoding=\"UTF-8\"?>
Therefore, when I try to submit the XML feed, the website displays the following error:
| Fatal error: Uncaught exception 'ErrorException' with message 'SimpleXMLElement::__construct(): Entity: line 1: parser error : String not started expecting ' or "'
I've tried using the stripslashes method from PHP, but it doesn't work, as if I try an echo after adding it an Order ID it displays the original XML without slashes, so I'm guessing there is a calling to some other function when using fillField that does indeed add the backslashes to my text, but I haven't been able to find the source for it.
Does anyone know where this conversion from " to \" is made in order to avoid it?
Thanks
Make sure the Magic Quotes setting is off in PHP, especially on the server you're submitting to. They are removed as of PHP 5.4.0.
Magic Quotes
Ensure that you are using stripcslashes instead of stripslashes in the page that receives the form.
Related
I am using Wordpress's XMLRPC API and the IXR_Library php class for WP API. wp.newPost is working normally if I use plain text or just simple text in body/content of the post but when I am posting a autocreated full html/shortcode content, it always gives me this error
Array ( [faultCode] => -32700 [faultString] => parse error. not well formed )
The content I am trying to post is a post content I normally use within WP with shortcodes but I want to post it via php using API since I am trying to automate my blogging.
You can check the body/content I am trying to post below
http://pastebin.com/U94XVZGT
Thats the content thats shown in XML debug mode (probably seems already html encoded)
Below is the full XML call and response returned via debugging, have removed user/pass from it
http://pastebin.com/26Nyx97K
I would really appreciate any possible help, I have already tried php's htmlentities and htmlspecialchar functions to encode my body and even tried str_replace to replace[ ] brackets in thought that they might be causing it
Again, its certainly due to the content code as I tried just a Hello World plain content and it worked ------------------------------------------------------------
A Update, earlier I was getting the html content from a textarea using $_POST, just now I tried to use same content in same variable but used ' ' single quotes instead around the content body and now posting works but I get a error
Warning: strpos(): Empty delimiter in wp-includes/class-wp-xmlrpc-server.php on line 4338
So if I use $body=$_POST['body']; it doesnt works and gives main error but directly using $body=' htmlcontent inside '; works but gives above strpos error
Please try adding the xml package:
sudo apt-get install php-xml
, this has resolved my problem.
TO anyone else having this problem, I fixed it. At end of it seemed the problem was due to wrong character encoding, I needed to Pass UTF8 but the content was being passed in some other char encoding (windows one) making some text and other languages not parseable, I modified my code to make it UTF 8 and now it works
I've changed my password to contain no symbols and it solved the issue.
I'm still not sure what broke everything initially but you might as well try changing it and trying again and see if that works for you.
To anyone else having this problem, it might also relate to WordPress bug #18310.
There is a patch for WordPress available at http://core.trac.wordpress.org/attachment/ticket/18310/18310-3.diff
See the WordPress bug report on their trac install at http://core.trac.wordpress.org/ticket/18310
I am using WAMPServer 2.1 on my local machine. I am trying to create a simple PHP script to output an XML file. I cannot get it to work without generating an error on IE and Chrome. On Firefox 4, it works.
Here is the PHP:
<?php
main();
function main()
{
header("Content-type: application/xml");
OutputXML('xml/login_user_good.xml');
exit(0);
}
function OutputXML($filename)
{
echo file_get_contents($filename);
}
?>
Here is the XML file:
<?xml version="1.0" encoding="utf-8" ?>
<login_user>
<result_code value="1"/>
<error value="Invalid username or password"/>
</login_user>
When I tried to access my PHP script using IE 8, I get this error:
The XML page cannot be displayed Cannot view XML input using style sheet.
Please correct the error and then click the Refresh button, or try again later.
--------------------------------------------------------------------------------
Invalid at the top level of the document. Error processing resource
'http://localhost/kiamobile/login_user.php'. Line 1, P...
<?xml version="1.0" encoding="utf-8" ?>
^
On Google Chrome 10, I get this error:
This page contains the following errors:
error on line 1 at column 6: XML declaration allowed only at the start of the document
On Firefox 4, I get a properly formatted XML output.
On any of the browsers, when I View Source, I can see the correct XML.
I have tried with and without the explicit header. If I remove the call to the PHP header() function, I get these results:
IE 8: Same error as before
Firefox 4: Blank screen
Chrome 10: Blank screen
Again, if I View Source in any of the browsers, I can see the XML. I have tried using text/html instead of application/xml, with no effect.
I have tried generating the XML directly in the PHP code, like this:
<?php
main();
function main()
{
header("Content-type: application/xml");
OutputXML();
exit(0);
}
function OutputXML()
{
echo <<<END
<?xml version="1.0" encoding="utf-8" ?>
<login_user>
<result_code value="1"/>
<error value="Invalid username or password"/>
</login_user>
END;
}
?>
When I do this, I get these results:
IE 8: Same error
Firefox 4: I see the formatted XML
Chrome 10: Blank screen
What am I doing wrong?
Your script may contain unwanted and invisible chars (like BOM) before PHP start tag
Browsers are not made for displaying just any arbitrary XML. They like HTML (which can be XHTML). It is unclear why you want browsers to display those. If it is intended for an and user then you should format those in HTML, if it is for a API (service) then check it with curl/wget/etc and I'm sure the XMl will look the same every time.
Recently I get an exception while I try to implement a special template engine.
Mine problem with DOMDocument load is in fact that I use some place holders in href and it will be replaced after with real values. I want to store that template and to use it each time when I need it, here is code sample
$this->dom = new DOMDocument;
$load_html = $this->dom->loadHTML($html);
And nesty part of HTML looks like
Visit our web site
and exception is
Fatal error: DOMDocument::loadXML() [<a href='domdocument.loadxml'>domdocument.loadxml</a>]: EntityRef: expecting ';' in Entity
I was try to find a some way to skip that check but couldn't find anything.
This causes no error in PHP 5.3. You must have some other HTML that is causing this. Normally you see this when you use an entity with no ; after it.
Foo   Bar
That throws that same error for me. Look for some entity without a ; on it. Browsers will render that, but it is not correct.
I found what was the problem. In my URL it was & sign, I was replace it with & and now it works.
I am currently trying to write some unit test against my zend framework controller. When I run the following code I receive this error:
public function testListActionShouldContainListTable()
{
$this->loginToSystem();
$uri = $this->_uriBase . 'campaign/list';
$_SERVER["REQUEST_URI"] = $uri;
$this->dispatch('/campaign/list');
$this->assertController('campaign');
$this->assertAction('list');
$this->assertQueryCount('#list',1);
}
CampaignControllerTests::testListActionShouldContainListTable
DOMDocument::loadHTML(): ID alrt already defined in Entity, line: 36
This occurs using any of the assertQuery and assertQueryContains methods. I have searched around but am not really finding a good answer to why it won't allow me to find this html node or how to get around this error.
Thanks in advance for any help!
Make sure the document is valid. You have duplicate IDs in your HMTL document.
The HTML being generated by the dispatch is bad (it looks like it has two ids being assigned to a single tag). This is not an error of your test (it's working fine), it's an error in your html output generated by the test...
I am trying to generate a dynamic xml document in CakePHP to output to the browser.
Here is my controller code:
Configure::write ('debug', 0);
$this->layout = null;
header('Content-type: text/xml');
echo "<?xml version=\"1.0\"?>";
View is something like this:
<abc>
something
</abc>
The output is probably as expected:
<?xml version="1.0"?><abc>something</abc>
The only problem is that there is a space before <?xml giving me an error:
XML Parsing Error: XML or text declaration not at start of entity
Line Number 1, Column 2:
<?xml version="1.0"?><abc> something </abc>
-^
I know this problem in PHP, when you have php-start and end tags it leaves a space and creates problems, so, I tried to move the line echo "<?xml ver... to controller from the view to avoid that but it didn't help.
Thanks in advance.
-happyhardik
Yes, the problem should be an space after the php end tag somewhere.
As the php end tag is not mandatory, remove any end tag in all your models (if there're any), the controller you're asking about, from app_controller.php and app_model.php and from your view helpers... It should be somewhere but it is not easy to find
EDIT: In fact it could be also an space before the php begin tag, look into those files and check that the begin tag is at the absolute beginning of the file
EDIT AGAIN: There are people that have created some scripts for doing that automatically for you, take a look to:
http://ragrawal.wordpress.com/2007/11/07/script-for-removing-blank-spaces-before-and-after-php-tags/
Actually, I find that it is most often a space AFTER the closing ?> tag in the layout file.
Also you should know that if you use the RequestHandler component and Router::parseExtensions( 'xml' ) in your routes.php you will automatically get the XmlHelper for use in your xml views.
The XmlHelper has a few neat functions in it. Check it out.
<?php
echo( $xml->header( ));
// outputs <?xml version="1.0" encoding="UTF-8" ?>
?>
The links for RequestHandler Component and the XmlHelper
http://book.cakephp.org/view/174/Request-Handling
http://book.cakephp.org/view/380/XML
Even though this does not answer the question directly. I thought it would be worth mentioning how easy it is to create dynamic XML views automatically using the CakePHP JSON and XML views helper, just in case people don't want to be doing it manually as seem to be the case above.
Step one: Add Router::parseExtensions(); to your routes.php file
Step two: Ensure the RequestHandler component is included in the relevant countroller by adding public $components = array('RequestHandler');
Step three: Now we only have to load some data and then display the data as XML or JSON automatically. Add something like the below:
public function xml_view () {
$this->set('data_array', $this->Model->find('all'));
$this->set('_serialize', array('data_array'));
}
That's literally all we need to do to generate an XML or JSON respone for the xml_view action. Not even necessary to set up a view file. When your request is .../controller/xml_view.xml then CakePHP will return an XML document, and when .json is the extension, a JSON response will be generate. So easy I can't believe it!