For most of China's history its view on global trade has been somewhat lopsided. On the one hand, China had a lot of cool stuff to export that the rest of the world really wanted. Silk, porcelain, and tea just to name a few. Even as early as Roman times, China was the only source for many of these luxury goods, and thus benefited from the flow of silver and gold into the country. However, while China was pretty cool with the whole exporting thing, it was much less so with idea of imports. After all, imports meant that gold and silver would flow out of the country, and China was all about that bling. To limit imports, China restricted overseas trade to only a few cities in southern China and required all transactions to be done through Chinese merchants. In this way the Chinese rich could have access to cool things from Europe, but not to a level that would reverse the flow of gold.
This system lasted for several hundred years until the British entered the picture. The British had a real hard on for laissez faire economics, and the world's most powerful navy to back them up. The very thought of China's restricted trade put them into a tizzy, but not nearly as much as the fact that the widespread popularity of Chinese tea in England was emptying the country's coffers of silver and gold. To top it all off, Britain was also losing money on its colonies in India, because cotton farmers there were unable to compete with the slave fueled cotton production of the United States. However, the British, being clever bastards, fixed both of these problems in one fell swoop.
That fell swoop was opium. Around 1780 British merchants shipped the first cargo of Indian opium to China, which proved quite popular amongst the Chinese given that opium (which is what morphine and heroine are derived from) apparently is pretty awesome to smoke (you know, if you just ignore all of the negative health effects). It was the perfect answer. Farmers in India could grow poppies instead of cotton, and selling opium to China slowed the flow of gold and silver out of British coffers.
Now opium smoking wasn't a new thing in China, but the stuff the British brought from India could be had for a much cheaper price, which as one can imagine, resulted in more people smoking opium. This trend was further exacerbated when the Americans, not to be outdone, began bringing in lower quality opium from Turkey, driving down prices and making the drug even more affordable for the Chinese masses.
At first the Chinese government was pretty cool with the whole thing, mostly because the British selling opium led to them having more money to buy tea. However, they soon became much less okay with it when an estimated 25 percent of their male population became addicted to it (fun fact, opium addicts aren't exactly the most productive people in the world), and more importantly, because it was reversing the flow of gold and silver from into China to out of China. In 1838 China declared opium imports illegal and started seizing and destroying cargoes. Britain responded by sending its navy to China to blow the shit out of everything within cannon range of the coast for the next three years in what became known as the First Opium War, which given technological differences at the time, was a lot like a grown man beating up a kid in middle school. The Chinese were forced to sign a treaty in 1842, reopening the opium trade, opening many new ports for trade, and giving the British the island of Hong Kong.
Now obviously this solved nothing for China, so they spent the next 14 years attempting to modernize their military so that it wouldn't be so easy to have their asses handed to them. It was basically a Rocky training montage for a whole country. It was a long 14 years of getting bullied by every western country imaginable, but finally in 1856 China was ready to do some flexing of its own. What followed was known as the Second Opium War, which was basically another four years of China getting its ass beat. In the end, China, fully humiliated, did the only thing it could, legalize the production of opium within its own borders. While this did nothing to stop China's huge opium addiction problem, it did at least ensure that people were smoking a home grown product, ending the need to import it from India.