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
MVP Betts plans to improve consistency in offseason
Even after winning MVP, Mookie Betts is focusing on improving his offensive consistency in the offseason.
Veteran catcher Jeff Mathis agrees to two-year deal with Rangers
Jeff Mathis, one of the top defensive catchers in the majors, has agreed to a two-year deal with the Rangers, according to The Athletic.
Yelich: 'Unbelievable feeling' being named NL MVP
Christian Yelich joins SportsCenter after being named the National League MVP and receiving 29 of 30 first-place votes.
Newly crowned MVP Mookie Betts staking a claim as MLB's best player
Winning baseball's biggest award adds an exclamation point: There might be nothing that Boston's right-field star can't do on the diamond.
Yankees GM: Hustling 'always a prerequisite,' but won't discuss Manny Machado
While saying that hustling "is always a prerequisite" when signing players, Yankees GM Brian Cashman doesn't want any part of a debate about free agent Manny Machado
Brewers' Christian Yelich beats out Nolan Arenado, Javier Baez for NL MVP award
Brewers slugger Christian Yelich topped fellow finalists Nolan Arenado of the Rockies and Javier Baez of the Cubs for the National League MVP award Thursday.