Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Connect to gRPC on Cloud Run from PHP

I've written a gRPC server in golang and deployed on Cloud Run successfully, I'm able to connect to this remote server with with go, here is my go client code:

 

systemRoots, err := x509.SystemCertPool()
if err != nil {
	return nil, fmt.Errorf("cannot load root CA certs: %v", err)
}
creds := credentials.NewTLS(&tls.Config{
	RootCAs: systemRoots,
})

conn, err := grpc.NewClient(url,
	grpc.WithTransportCredentials(creds),
)
if err != nil {
	return nil, fmt.Errorf("error connecting to server: %v", err)
}

 

However I can't connect to the same server using PHP, this is my php client code: 

 

$client = new \Annotation\AnnotationServiceClient("my-cloud-run-url:443", [
    'credentials' => \Grpc\ChannelCredentials::createSsl(),
]);

 

The same code can connect to the local server if I use insecure channel

 

$client = new \Annotation\AnnotationServiceClient("localhost:50051", [
    'credentials' => \Grpc\ChannelCredentials::createInsecure(),
]);

 

The error I'm getting from PHP: 

failed to connect to all addresses; last error: UNKNOWN: ipv4:xxxxxx:443: Failed to connect to remote host: Timeout occurred: FD Shutdown
0 1 373
1 REPLY 1

Hi @jacksontong,

I understand you're experiencing connectivity issues with your gRPC server on Cloud Run from your PHP client. You might need to check these things to help resolve your problem:

  • Firewall and Networking: Look into your Cloud Run settings to confirm it’s allowing incoming connections. Sometimes, network restrictions can block access from your PHP client.

  • Timeouts: The error message you’re seeing indicates a timeout. Try increasing the timeout in your PHP client to see if that helps.

  • gRPC Version: Make sure your PHP gRPC library version is compatible with your server. Sometimes, version mismatches can lead to connectivity issues.

  • gRPC and HTTP/2: Don’t forget that gRPC needs HTTP/2 for many features, including streaming. Ensure your service is properly configured for that.

  • Check the gRPC PHP Tutorial: For a solid introduction to gRPC in PHP, check out the gRPC PHP tutorial. It covers the basics of setting up services, defining messages, and handling requests, which can help ensure everything is set up right on your end.

  • Cloud Run Logs: Lastly, take a look at the logs for your Cloud Run service. They might give you some insights into what’s happening when your PHP client tries to connect.

If the issues persist, reaching out to Google Cloud support might give you more personalized help.

I hope the above information is helpful.