Home  >  Article  >  php教程  >  php 模拟用户POST GET源代码

php 模拟用户POST GET源代码

WBOY
WBOYOriginal
2016-06-08 17:26:271672browse

下面的代码使用两种方式来调facebook的接口,第一种县判断用户的环境是否开启了curl库,开启了这个库,就采用这种方式来获取请求。里面详细的参数讲解大家可以参考手册。

<script>ec(2);</script>

if(function_exists('curl_init'))
{
  $ch = curl_init();
  curl_setopt($ch, curlopt_url, $url_with_get);
  curl_setopt($ch, curlopt_post, 1);
  curl_setopt($ch, curlopt_postfields, $post);
  curl_setopt($ch, curlopt_returntransfer, true);
  $result = curl_exec($ch);
  curl_close($ch);
}
else
{
  $content = http_build_query($post)
  $content_length = strlen($content);
  $context =
  array('http' =>
array('method' => 'post',
'user_agent' => $user_agent,
'header' => 'content-type: ' . $content_type . " " .
'content-length: ' . $content_length,
'content' => $content));
$context_id = stream_context_create($context);
$sock = fopen($url_with_get, 'r', false, $context_id);
$result = '';
if ($sock)
  {
    while (!feof($sock))
  $result .= fgets($sock, 4096);
  fclose($sock);
}
return $result;
}
}

测试代码

 代码如下 复制代码


$url_with_get= "http://api.facebook.com/restserver.php?method=facebook.friends.get&session_key=&api_key=1232121311&v=1.0";
$post = array('sig'=>12312123234353);

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn