4

I'm creating an application for Google App engine, where CURL isn't 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 would really, really appreciate it if anyone with more experience could help me out.

The plan was to convert the following CURL requests to urlFetch. If anyone can point me in the right direction, or propose a better alternative, I'd greatly 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;}
Mats Bakken
  • 155
  • 1
  • 14
  • 1
    May I suggest the [Artax HTTP client](https://github.com/rdlowrey/Artax). It's light-years better than the *awful* `curl_*` API and guess what? No libcurl dependency so it will work out of the box on any sane PHP install. –  Aug 09 '13 at 05:06

4 Answers4

9

Did you look at the Urlfetch documentation and the linked PHP article about wrappers?. You can experiment with this live shell.

The code could be translated to 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;
}
Sebastian Kreft
  • 7,819
  • 3
  • 24
  • 41
  • Thank you for your answer! I did look at the documentation, but I couldn figure out how it would be translated. I'll have a look when I come home. Thanks a lot for your help! – Mats Bakken Aug 08 '13 at 10:41
  • oh! the mini-sell from the SDK samples :D, cool enough as i do not need to upload mine as i planned :D – m3nda Jun 20 '14 at 09:20
6

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

user3275764
  • 61
  • 1
  • 1
  • This is just awesome! Do not need to rewrite any piece of code. Amazing stuff. – m3nda Jun 20 '14 at 09:22
  • I'd try and is not working on Glype PHP webproxy script. Maybe there's too much cURL code inside, some unsupported functions. Seems to cover a few functions. Really interested on this type of solution :O – m3nda Jun 20 '14 at 09:54
2

Someone here suggested using the PURL from azayarni. Let me warn you: avoid using it on Google App Engine. I spent SEVERAL days trying to get it work without success: the Google PHP Client SDK is rewriting itself some CURL functions and simply PURL mess it up a lot. Some things were working, some were not. The URLFETCH tool is much more easier and safe.

1

Its a very old post, but just an update: Google App Engine now supports cURL with its PHP 5.5 runtime.

Kos
  • 4,890
  • 9
  • 38
  • 42
BiJ
  • 1,639
  • 5
  • 24
  • 55