PHOENIX -- Madison Bumgarner labored through a duel of aces, working through traffic in each of his five innings.
"That's one of those games my back's kind of against the whole time I'm out there," Bumgarner said. "Had to work extra hard, but at the same time those kind of games are gratifying to keep your team in it."
Bumgarner didn't have his best stuff, but got the outs -- with some help -- when he needed them. Longoria made a diving stop with the bases loaded in the first inning and Bumgarner (4-4) limited the Diamondbacks to a run on seven hits while stranding nine runners.
"It saves you, a play like that, stops a rally," Giants manager Bruce Bochy said. "That ball gets through, they get momentum going."
Longoria also hit a solo homer off Greinke (12-6) and San Francisco broke open a close games five runs in the eighth inning. Austin Slater had two RBI and scored three times for the Giants, who have won four straight to stay within five games of the Los Angeles Dodgers in the NL West.
Greinke gave up two runs on four hits and struck five in six innings to lose for the first time in nine starts.
Arizona's David Peralta had four hits after missing the previous two games with right shoulder tightness.
"We hit the ball hard. I thought we were patient," Diamondbacks manager Torey Lovullo said. "We just couldn't get that big hit or have that big moment at the right time."
Greinke, NL pitcher of the month for July, went 7-0 with a 1.53 ERA and 53 strikeouts in eight starts since June 18.
The right-hander had been using his slow curveball effectively this season, but Longoria took advantage of one in the first inning, hooking a 69 mph pitch just inside the foul pole in left for a solo homer.
Bumgarner also helped himself with a run-scoring single in the fifth inning off Greinke.
"The stuff was good, but the command probably wasn't as good as the last however many starts," Greinke said.
ZIELGER RIDES THE CART
The Diamondbacks brought back an old tradition this season with a bullpen cart. One problem: None of the Diamondbacks wanted to use it.
Brad Ziegler changed that when he entered the game in the seventh inning, drawing cheers from the hometown fans. Only a handful of opposing players used the cart during games, including Ziegler as an opposing player with Miami before being traded back to Arizona.
"I appreciated the ovation the fans gave me," said Ziegler, who allowed four runs on three hits in an inning. "It is nice to feel welcome."
With Ziegler struggling and a left-hander at the plate, Lovullo went out to replace him in the eighth inning. He couldn't, thanks to a mistake by the umpire crew.
After Arizona pitching coach Mike Butcher made a trip to the mound, the Giants brought in pinch hitter Alen Hansen. Lovullo came out of the dugout to ask the umpires if he could then switch pitchers, but was told he couldn't.
"He was told that he could not, when actually there's a caveat rule that says when there's a trip to the pitcher and then the guy leaves the mound and the offensive team then substitutes, it is then permissible by rule to then go to the mound to remove the pitcher," crew chief Bill Welke said.
Hanson followed with a run-scoring single in San Francisco's five-run inning.
Arizona optioned RHP Silvino Bracho to Triple-A Reno to make room for Ziegler's return.
Giants: INF Pablo Sandoval will have season-ending surgery for a right hamstring tear suffered while sliding at home plate against San Diego on Sunday.
Diamondbacks: 3B Jake Lamb opted to have season-ending surgery on his left shoulder to repair fraying of the rotator cuff. He was not in pain, but the damage limited his ability to swing a bat.
Diamondbacks LHP Patrick Corbin is second in the NL with 166 strikeouts heading into Friday's game against the Giants. San Francisco will send RHP Chris Stratton to the mound after a season-ending injury to Johnny Cueto forced manager Bruce Bochy to juggle his rotation.
More AP baseball: https://apnews.com/MLBbaseball
Alex Bregman knows how to hold a grudge, saying he wears No. 2 for the Astros partially because the Diamondbacks didn't draft him No. 1 overall in 2015.
Three straight losses to the Kansas City Royals has left a sour taste in the Minnesota Twins' mouths as they prepare to kick off their final road trip of the season, a 10-game, three-city jaunt that will include stops in Detroit and Oakland.
With just over two weeks left in the regular season, the Milwaukee Brewers are still nipping at the Chicago Cubs' heels in the National League Central race while trying to protect their wild-card lead from the resurgent Los Angeles Dodgers.
Marco Gonzales will be glad to see the Los Angeles Angels again.
Texas manager Jeff Banister has decided to play it straight Sunday afternoon as the Rangers go for a sweep of a three-game series against the San Diego Padres at Petco Park.
One day after knocking Colorado out of first place, the San Francisco Giants go for a three-game sweep Sunday afternoon in a game that could drop the Rockies out of playoff position.