GLENDALE, Ariz. (AP) Rookie Josh Rosen came into the huddle to start Arizona's final drive with a few choice words.

''We're going to win this (expletive) game,'' he said.

And the Cardinals did just that.

Rosen threw a 9-yard touchdown pass to fellow rookie Christian Kirk with 34 seconds to play to rally the Arizona Cardinals to an 18-15 victory over the San Francisco 49ers on Sunday in a matchup of teams with two of the worst records in the NFL.

''That was a lot of fun,'' Rosen said. ''That's why sports are fun.''

The Cardinals, winners at home for the first time under first-year coach Steve Wilks, beat San Francisco for the eighth straight time and second time in three weeks.

''Our defense gave us a very good chance to win that, giving us three turnovers and getting us a 12-point lead,'' San Francisco coach Kyle Shanahan said. ''We didn't capitalize on them enough for touchdowns and we didn't make the plays at the end when we needed to.''

Arizona (2-6) got two fourth-quarter touchdown passes from Rosen after the 49ers (1-7) had built a 15-3 lead.

Rosen's 13-yard pass to Larry Fitzgerald cut the lead to 15-10 with 11:06 to play. Fitzgerald also caught a 2-point conversion pass after Kirk's TD.

Fitzgerald, in his 15th NFL season, caught eight passes for 102 yards, his best game of the season. He had four catches for 81 yards in the fourth quarter and the crowd chanted ''LAR-RY, LAR-RY.''

''I threw him the ball and he caught it, whether it was a good ball or a bad ball,'' Rosen said. ''That's why he's a first-ballot Hall of Famer. I'm going to be telling my kids one day that I got to throw to him.''

Fitzgerald even spiked the football after the 2-point catch, something he said he'd never done before.

''We don't spike the ball in our house,'' Wilks said, ''but I'll let that one slide. I think he got carried away.''

Fitzgerald joked that he'd been upset because his oldest son had chosen to go to the state fair instead of the game.

C.J. Beathard connected with Marquise Goodwin on a 55-yard play for the 49ers' only touchdown.

San Francisco was slogging along with a 5-3 lead before Goodwin beat Patrick Peterson and took a short pass over the middle, then raced to the end zone to make it 12-3 in the third quarter. Robbie Gould's second field goal of the game, from 20 yards, boosted the lead to 15-3.

San Francisco appeared to get a critical turnover with 4:44 to play, needing a coach's challenge to get it. Arizona tight end Jermaine Gresham fumbled along the sideline and the 49ers' Fred Warner recovered at the San Francisco 31. Officials initially ruled a 49er touched the ball while out of bounds, but the 49ers challenged and replays showed the player tried but failed to touch it.

But the Niners couldn't move the ball and Arizona went 73 yards in 12 plays for the winning score, using 1:42.

The two TD drives came under the direction of new offensive coordinator Byron Leftwich, who was promoted from quarterbacks coach after Mike McCoy was fired.

''We really didn't change much in the offense,'' Rosen said. ''One thing I think Byron's really good with is just his demeanor. ... He's got really good timing with a lot of his play calls. He's got that poise. He's a steadying force. It wasn't a pretty one by any means, but baby steps here.''

San Francisco scored first when, backed up deep in Arizona territory, Rosen was called for intentional grounding in the end zone. The resulting safety made it 2-0.

The only real scoring drive of the first half resulted in Dawson's 31-yard field goal that put Arizona up 3-2. The 12-play, 78-yard drive was kept alive by a third-down pass interference penalty on Reuben Foster.

The only turnover of the first half set up San Francisco's go-ahead field goal. Jaquiski Tartt stepped in front of intended receiver Kirk and intercepted Rosen's pass, returning it 21 yards to the Cardinals 12. But the 49ers couldn't move the ball and Gould's 27-yard field goal put San Francisco ahead 5-3.

FITZ HITS 112

Fitzgerald's 112th career touchdown catch moves him past Tony Gonzalez alone into seventh on the NFL career list. He's caught a TD pass in each of his last two games after getting none the first six games of the season. He needs three to tie Antonio Gates for sixth.

INJURIES

The already beat-up 49ers lost two important defensive players.

Foster left early with a hamstring injury. Tartt was knocked out with a shoulder injury.

49ers defensive back Amare Exum Jr. left in the fourth quarter with a head injury.

UP NEXT

49ers: host Oakland on Thursday night.

Cardinals: have bye week, play at Kansas City on Nov. 11.

---

More AP NFL: https://apnews.com/tag/NFL and https://twitter.com/AP-NFL

Copyright 2018 by AP. Any commercial use or distribution without the express written consent of AP is strictly prohibited.