Replacing CURL with urlfetch in PHP

I am creating an application for the Google App engine where CURL is not allowed. As far as I know urlFetch is the best alternative.

I don't know if I can achieve the same result with urlFetch, but I really would really appreciate it if someone with a lot of experience could help me.

The plan was to convert the following CURL requests to urlFetch. If someone can point me in the right direction or suggest a better alternative, I would really appreciate it.

public function postCall($endpoint, $post_data, $param1, $param2, $json=1, $headers=false) { $ch = curl_init(); curl_setopt($ch,CURLOPT_URL, $this->options['url'].$endpoint); curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1); if ($headers && is_array($headers)) { curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); } $post_data['req_token'] = $this->hash($param1, $param2); curl_setopt($ch, CURLOPT_POST, count($post_data)); if (!$headers) curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_data)); else curl_setopt($ch,CURLOPT_POSTFIELDS, $post_data); $this->debug('POST params: ' . json_encode($post_data)); $result = curl_exec($ch); if ($result === false) { $this->debug('CURL error: '.curl_error($ch)); return false; } $this->debug('HTTP response code' . curl_getinfo($ch, CURLINFO_HTTP_CODE)); $this->debug('POST return ' . $result); // close connection curl_close($ch); if ($json) return json_decode(utf8_encode($result), true); else return $result;} 
+4
source share
4 answers

You looked at the Urlfetch documentation and the related PHP wrapper article . You can experiment with this live shell .

The code can be translated into something like:

 public function postCall($endpoint, $post_data, $param1, $param2, $json=1, $headers=false) { $post_data['req_token'] = $this->hash($param1, $param2); $this->debug('POST params: ' . json_encode($post_data)); $data = http_build_query($post_data); $options = array("http"=> array( "method" => "POST", "content" => $post_data, ) ); if ($headers && is_array($headers)) { $options["http"]["header"] = $headers; } $context = stream_context_create($options); $result = file_get_contents("http://app.com/path?query=update", false, $context); if ($result === FALSE) { $this->debug('Error: '. print_r($http_response_header)); return FALSE; } $this->debug('Response headers:' . print_r($http_response_header)); // To get the status code you would need to parse that response $this->debug('POST return ' . $result); if ($json) return json_decode(utf8_encode($result), true); else return $result; } 
+9
source

Here is a simple library that replaces native curl functions with urlfetch. https://github.com/azayarni/purl

+6
source

Someone here suggested using PURL from the Azayarn. Let me warn you: do not use it in the Google App Engine. I spent SMALL days trying to make it work without success: the Google PHP Client SDK rewrites some CURL functions for itself, and just PURL really scares him. Some things worked, and some did not. The URLFETCH tool is much simpler and safer.

+2
source

This is an old post, but just an update. The Google app engine now supports cURL with its php 5.5 working environment.

0
source

Source: https://habr.com/ru/post/1495849/


All Articles