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
New Mets infielder Jed Lowrie to have MRI on knee
Jed Lowrie will get an MRI after experiencing pain in the back of his knee while running.
Over/unders, World Series and playoff odds for all 30 MLB teams
The Baltimore Orioles have a season win total of 59.5, while the Yankees are at 96.5. Here are the odds you need to know for all 30 MLB teams.
Stephen A.: Almost no one in sports is worth 10-year deal
Stephen A. Smith has a problem with Manny Machado's 10-year deal with the Padres because he isn't a "box-office" player who can sell himself.
Buster's Buzz: Why Machado signing is a test for Padres ownership
San Diego made a surprising free-agent splash for the second consecutive winter. But can the Padres surround Manny Machado and Eric Hosmer with a championship-level roster?
What does Machado's deal mean for Harper?
Buster Olney sees Manny Machado's $300 million contract as a win for Bryce Harper's free agency.
Stanton glad to play meaningful games 'past May 7th'
Giancarlo Stanton says the biggest difference about coming to New York is playing meaningful games in May and that postseason success is what is driving him.