search
Homephp教程php手册PHP调用小黄鸡 api post发送

下面这段代码有涉及到io操作,因而bae,跟sae上不能试用。 可以参考后面的那个 curl版本。那个是通用的。不过显然下面这种IO操作的写法执行起来速度快一点~ 首先得声明我没进行过测试。 能抓取数据的话,很多事都很容易做到。不管是天气,地理信息之类的查询


下面这段代码有涉及到io操作,因而bae,跟sae上不能试用。  可以参考后面的那个 curl版本。那个是通用的。不过显然下面这种IO操作的写法执行起来速度快一点~

首先得声明我没进行过测试。

能抓取数据的话,很多事都很容易做到。不管是天气,地理信息之类的查询。  下面几种方法都是post发送取得回调的response内容。

<!-- index.php -->页面
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<?php function do_post_request($url, $data, $optional_headers = null)
  {
     $params = array('http' => array(
                  'method' => 'POST',
                  'content' => $data
               ));
     if ($optional_headers !== null) {
        $params['http']['header'] = $optional_headers;
     }
     $ctx = stream_context_create($params);
     $fp = @fopen($url, 'rb', false, $ctx);
     if (!$fp) {
        throw new Exception("Problem with $url, $php_errormsg");
     }
     $response = @stream_get_contents($fp);
     if ($response === false) {
        throw new Exception("Problem reading data from $url, $php_errormsg");
     }
     return $response;
  }
  function simi($value="Love"){
  	$url = 'http://www.xiaohuangji.com/ajax.php';
  	$postdata = array("para"=>$value); 
  	$postdata = http_build_query($postdata);
  	return do_post_request($url,$postdata);
  }echo simi("Love");
?>


调用这个这段的代码就是
// 装载小黄鸡函数
// @author ipqhjjybj
// @data  2013.11.30
// 调用方法:
// $result = simi($str);
// $result String类型  是返回的内容文本
// $str String类型  是要回答的内容


也可以用CURL来做 


下面这段代码是可以直接放在任意的一个bae或者sae 上,可以做微信公众平台开发,也可以做其他的交流途径。 Curl的写法在哪里都是支持的~


function curl_post($url, array $post = NULL, array $options = array()) 
{ 
    $defaults = array( 
        CURLOPT_POST => 1, 
        CURLOPT_HEADER => 0, 
        CURLOPT_URL => $url, 
        CURLOPT_FRESH_CONNECT => 1, 
        CURLOPT_RETURNTRANSFER => 1, 
        CURLOPT_FORBID_REUSE => 1, 
        CURLOPT_TIMEOUT => 4, 
        CURLOPT_POSTFIELDS => http_build_query($post) 
    ); 

    $ch = curl_init(); 
    curl_setopt_array($ch, ($options + $defaults)); 
    if( ! $result = curl_exec($ch)) 
    { 
        trigger_error(curl_error($ch)); 
    } 
    curl_close($ch); 
    return $result; 
} 

function simi($value = "Love")
{
	$post_data = array ("para"=>$value) ;
    $url ='http://www.xiaohuangji.com/ajax.php';
    return curl_post($url,$post_data);
}
// 调用方法
// $result = simi("你的数据");
echo simi("Love");

也可以这样
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<?php function simi($value = "Love"){
    $post_data = array ("para"=>$value) ;
    $url ='http://www.xiaohuangji.com/ajax.php';
    $o = "" ;
    foreach ( $post_data as $k => $v )
    {
        $o .= "$k=" . urlencode ( $v ) . "&" ;
    }
    $post_data = substr ( $o , 0 ,- 1 ) ;
    $ch = curl_init () ;
    curl_setopt ( $ch , CURLOPT_POST , 1 ) ;
    curl_setopt ( $ch , CURLOPT_HEADER , 0 ) ;
    curl_setopt ( $ch , CURLOPT_URL , $url ) ;
    curl_setopt ( $ch , CURLOPT_RETURNTRANSFER , 1); // 这句还是需要的。。
//为了支持cookie 
//curl_setopt ( $ch , CURLOPT_COOKIEJAR , ' cookie.txt ' ) ;
    curl_setopt ( $ch , CURLOPT_POSTFIELDS , $post_data ) ;
    curl_setopt ( $ch , CURLOPT_USERAGENT , "ipqhjjybj's curl Example beta");
    $result = curl_exec ( $ch ) ;
    curl_close( $ch );
    $result = substr($result,0,-1);
    return $result;
}
// 调用方法
// $result = simi("你的数据");
echo simi("Love");
?>


解释下来源。用google抓包后是:



  1. Request URL:

    http://www.xiaohuangji.com/ajax.php

  2. Request Method:

    POST

    1. FormData
      para=TT


    Response 就直接是内容了

所以。。很简单。。


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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!