在 PHP 中用 urlfetch 替换 CURL [英] Replacing CURL with urlfetch in PHP

查看:18
本文介绍了在 PHP 中用 urlfetch 替换 CURL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为 Google App 引擎创建一个应用程序,其中不允许使用 CURL.据我所知,urlFetch 是最好的选择.

I'm creating an application for Google App engine, where CURL isn't allowed. As far as I know, urlFetch is the best alternative.

我不知道我是否可以使用 urlFetch 获得相同的结果,但如果有更多经验的人可以帮助我,我真的非常非常感激.

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.

计划是将以下 CURL 请求转换为 urlFetch.如果有人能指出我正确的方向,或提出更好的替代方案,我将不胜感激.

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;}

推荐答案

您是否查看了 Urlfetch 文档有关包装器的链接 PHP 文章?.您可以尝试使用这个 live shell.

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;
}

这篇关于在 PHP 中用 urlfetch 替换 CURL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆