SAN FRANCISCO — Jhonny Peralta knew he hit it well. He didn’t know how well until the ball landed in the seats.

Peralta’s two-run homer produced the first runs scored by St. Louis in its first at-bat since June 18, a span of 14 games, leading the Cardinals past the reeling San Francisco Giants 7-2 on Thursday.

“I’d heard from the guys that you need to hit it hard to get it out here,” Peralta said.

Carlos Martinez (2-3) earned his second win as a starter and added a two-run single for the Cardinals, who won their second straight after losing three in a row. Martinez allowed a run and five hits in five innings. He walked one and struck out six.

“We’ve had our struggles at the plate,” Peralta said. “It was good to start it off in the first inning.”

Madison Bumgarner (9-6) fell to 0-3 over his past four home starts after allowing five runs — four earned — and six hits over five innings. He walked three and struck out six.

Bumgarner also drove in a run for the Giants, who have lost 17 of 22 and fell out of first place in the NL West.

“You always want to play at home and I haven’t pitched very good here all year,” Bumgarner said. “I don’t know what it is. You can’t give in, you have to keep grinding.”

Help could be on the way. The Giants optioned first baseman Adam Duvall to Triple-A Fresno after the game in anticipation of recalling Brandon Belt in time for Friday’s game in San Diego.

“He’s somebody we like having out there,” Bumgarner said. “It’s not going to do anything but help us.”

Angel Pagan may return to the Giants lineup early next week, giving them some semblance of order at the top of the lineup.

Matt Carpenter continued ripping Giants pitching, extending his hitting streak against them to 11 games with a 2-for-4 effort. He has a .519 (28 for 54) career average against the Giants and has hit safely in 18 of his 20 games, including the postseason, against them.

“It’s one of those crazy things,” Carpenter said. “Luck has been on my side so far I guess.”

Yadier Molina added three hits and Oscar Tavares, John Jay and Shane Robinson each drove in a run for the Cardinals.

Peralta’s first-inning home run, after Carpenter drew a walk, put St. Louis ahead to stay.

“I was able to work the walk and Jhonny hit the home run,” Carpenter said. “That kind of set the tone and we found a way to pile it on.”

Bumgarner drove in a run for the Giants in the third, but then gave up three runs in the fourth — two on a single from Martinez.

Page 2 of 2 - Robinson, pinch hitting for Martinez in the sixth, tripled home a run. Tavares added an RBI in the seventh.

The Giants loaded the bases with two outs in the fifth, but Martinez got Buster Posey on a swinging strike three to end the threat.

In the sixth, Tyler Colvin walked, Adam Duvall singled and Brandon Crawford punched a single into right field. An error allowed Colvin to score.