(Reuters) - A bill to fund border security blew up in House Speaker John Boehner's face on Thursday, leaving Republicans in disarray and struggling to reconcile Tea Party demands with the need to deal with a humanitarian crisis on the southwestern border with Mexico.

Boehner, the top U.S. Republican, withdrew the bill after failing to corral a sufficient number of Republican votes for passage. That left him in an all-too-familiar position of having to somehow pass legislation or risk damage to his party in the November congressional elections in which Republicans are trying to capture the Senate.

A carefully crafted, $659 million bill to pay for more border security and help feed and house tens of thousands of Central American children arriving illegally in the United States unexpectedly collapsed on Thursday.

Tea Party-backed Senator Ted Cruz of Texas had his fingerprints all over Thursday's debacle for Boehner. Cruz, a potential 2016 presidential contender, had lobbied his House Republican colleagues to reject the legislation on the grounds that it was too timid.

The measure, complained Cruz, would not reverse President Barack Obama's 2012 policy of suspending deportations of undocumented residents who were brought to the United States as children by their parents.

Shortly after the drama unfolded on the floor of the House of Representatives, rank-and-file Republicans were publicly warring with each other. "Someday, Republicans will wise up and stop listening to Ted Cruz," a disgusted Representative Peter King of New York told reporters. Cruz and a handful of other Republicans, King said, "have hijacked the party."

Contrast that with a pleased Representative Michele Bachmann, the Minnesota Tea Party activist and failed 2012 presidential candidate.

"The people are very clear. They want people deported immediately," Bachmann said. "And they want to have the fence built up so that they don’t come in. They are tired of seeing their tax dollars spent on people who are here illegally in the United States."

All this as Boehner and moderate Republicans try to expand Republican Party appeal to the growing number of Hispanic voters who roundly rejected Republican presidential candidate Mitt Romney in 2012.

Thursday's theatrics were not the first time Cruz proved to be a thorn in Boehner's side.

The freshman Texas senator engineered last October's government shutdown when he prevailed upon House Republicans to withhold federal funds until Obama's signature healthcare law was either repealed or hobbled.

In the end, "Obamacare" was untouched, agency funding was restored after a damaging 16-day shutdown and public opinion polls showed that voters were disgusted with Republican tactics.

This latest dust-up over immigration, an issue that has dogged Boehner for years amid Republican rank-and-file opposition to comprehensive legislation, could foreshadow even tougher times in September.

That is when Congress returns from a five-week summer recess, a break that was supposed to have begun for House members on Thursday but was delayed by the border funding fight.

Boehner's newly-installed leadership team, one that was supposed to demonstrate improved ties with the party's conservative wing, sputtered on Thursday.

That team will be tested in coming days, as Boehner tries to figure out a way to finally pass a border-security funding bill, even if it dies in the Senate.

And then, in September, Republican leaders will face another test of whether they can win passage of a budget bill and avoid another government shutdown, just weeks before November elections when Boehner's party will be arguing that it can govern better than Obama. It's an argument Democrats are working hard to unravel.

The Democratic-led Senate is pushing a $2.7 billion border-security funding bill, which Republicans are lining up to oppose on the grounds that it spends too much and does not address the root of the Central American migration problem.

Senate Appropriations Committee Chairwoman Barbara Mikulski tried to contrast her party's approach to that of Republicans when, during Thursday's floor debate, she said she had spoken to a 15-year-old girl whose parents were killed in gang violence in Honduras.

"You're going to send her back?" Mikulski asked.