Yang Fugong, the director of palace communications, and Yang recommended Zhang to be Taichang Boshi (), a scholar at the ministry of worship (太常寺, Taichang Si). Zhang was subsequently made Duzhi Yuanwailang (), a low-level official at the ministry of census (戶部, Hubu). In 880, as the major agrarian rebel Huang Chao approached the Tang capital Chang'an, Zhang claimed to be ill, and he took his mother and his family to take refuge at Shang Prefecture (商州, in modern Shangluo, Shaanxi). When Huang subsequently attacked Chang'an, the powerful eunuch Tian Lingzi took then-reigning Emperor Xizong and fled toward Chengdu, where Tian's brother Chen Jingxuan served as the military governor (Jiedushi) of Xichuan Circuit (). Meanwhile, while Emperor Xizong was fleeing through Xingyuan (興元, in modern Hanzhong, Shaanxi), his train was running out of food supplies. Li Kang () the magistrate of Hanyin County (漢陰, in modern Ankang, Shaanxi) met the emperor, with several hundred mules bearing food, to supply the emperor. When Emperor Xizong asked how Li Kang, as a mere county magistrate, could think of this, Li Kang credited Zhang for reminding him. Thereafter, Emperor Xizong summoned Zhang to his presence and made him Bingbu Langzhong (兵部郎中, Bingbu Langzhong). Zhang thereafter ingratiated himself with Tian as well, including prostrating himself before Tian, but embarrassed himself on one occasion when Tian pointed this out in public. This also offended Yang, as he and Tian were rivals, particularly because Zhang, after ingratiating Tian, was no longer paying attention to Yang. Meanwhile, by 882, the chancellor Wang Duo was put in overall command of the operations against Huang Chao, and he went to Yicheng Circuit (義成, headquartered in modern Anyang, Henan) to oversee the operations. Zhang accompanied Wang and served on his staff. At that time, Wang Jingwu, who controlled Pinglu Circuit (平盧, headquartered in modern Weifang, Shandong), had accepted offices bestowed by Huang (who had declared himself the emperor of a new state of Qi), and when Wang Duo sent Zhang to Pinglu to try to persuade Wang Jingwu to join the imperial forces against Huang, Wang Jingwu initially refused to meet Zhang. Zhang rebuked Wang Jingwu, and Wang Jingwu subsequently allowed him to address the soldiers; Zhang then argued to the soldiers that the imperial forces would soon defeat Huang, and it was time for them to earn honors with achievements. With the soldiers agreeing with Zhang, Wang Jingwu rejoined