The influx of tens of thousands of unaccompanied children at our borders in Texas and other parts of the Southwest has proved a major political problem for the Obama administration, as well as a major opportunity for Republicans.

“This is a failure of diplomacy,” as Texas Gov. Rick Perry put it. “It is a failure of leadership from the administration.”

Republican congressmen — joined by not a few Democrats — are insisting that the administration respond to the crisis by deporting the tens of thousands of children immediately. They are also trying to frame the issue as evidence that Obama’s policy to try to halt illegal immigration has failed.

There are two major problems with that argument: It makes no sense, and the proposed “solution” is against the law.

Based on last year’s data, the vast majority of unaccompanied child refugees are coming from three countries — Guatemala (37 percent), El Salvador (26 percent) and Honduras (30 percent). (Those from Mexico are by law turned back immediately.) And they are coming because under Section 462 of the Homeland Security Act of 2002, signed into law by President Bush and amended in 2008, unaccompanied children fleeing here from those countries are required to be taken in so that their pleas for permanent refugee status can be considered.

In other words, they aren’t here illegally, and by law they can’t be sent back without a deportation hearing. In May alone, 9,700 children qualified for that protection; since October, the number exceeds 50,000, which means the system is swamped. And why are the children coming?

In part, their parents may be hoping that the children find opportunity that is not available to them in their home countries, which are some of the poorest in the world. In many cases, perhaps the majority, they are fleeing grotesque and well-documented gang violence in which thousands of children are being coerced to join or be killed.

Think about it: How desperate would your family’s situation have to be for you to even consider sending your children off on their own, across deserts and bandit-infested territory, in hopes they will be taken in by a foreign country?

This week, President Obama asked for an emergency appropriation of $3.7 billion to house the influx of child refugees, to accelerate the disposition of their cases, to tighten border security and to help source countries limit the exodus. So far, Congress has refused. But let’s put this into perspective:

In the Syrian civil war, some 3 million people — more than 75 percent of them children — have been forced to seek refuge in neighboring countries such as Lebanon, Jordan and Turkey. That number grows by an estimated 100,000 every month. Those countries have much smaller populations than the United States, far fewer resources with which to handle the influx and much less political and social stability.

So while this is a challenge, let’s not go feeling sorry for ourselves. I don’t know what the long-term answer is, but our response should begin with the recognition that this is not a wave of criminals invading our shore; they are children caught in a humanitarian crisis that tests our values and compassion as well as our political system.