google app engine - Replacing CURL with urlfetch in PHP -
i'm creating application google app engine, curl isn't allowed. far know, urlfetch best alternative.
i don't know if can achieve same result urlfetch, really, appreciate if more experience me out.
the plan convert following curl requests urlfetch. if can point me in right direction, or propose better alternative, i'd 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;}
did @ urlfetch documentation , linked php article wrappers?. can experiment live shell.
the code translated 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)); // status code need parse response $this->debug('post return ' . $result); if ($json) return json_decode(utf8_encode($result), true); else return $result; }
Comments
Post a Comment