标签 PHP 下的文章

AngularJs 的 $http 发送 POST 请求 , PHP 无法接收的解决方案

ngrApp.config(function ($stateProvider, $urlRouterProvider, $httpProvider){
    // Use x-www-form-urlencoded Content-Type
    $httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';
    /**
     * The workhorse; converts an object to x-www-form-urlencoded serialization.
     * @param {Object} obj
     * @return {String}
     */
    var param = function(obj) {
        var query = '', name, value, fullSubName, subName, subValue, innerObj, i;
        for(name in obj) {
            value = obj[name];
            if(value instanceof Array) {
                for(i=0; i<value.length; ++i) {
                    subValue = value[i];
                    fullSubName = name + '[' + i + ']';
                    innerObj = {};
                    innerObj[fullSubName] = subValue;
                    query += param(innerObj) + '&';
                }
            }
            else if(value instanceof Object) {
                for(subName in value) {
                    subValue = value[subName];
                    fullSubName = name + '[' + subName + ']';
                    innerObj = {};
                    innerObj[fullSubName] = subValue;
                    query += param(innerObj) + '&';
                }
            }
            else if(value !== undefined && value !== null)
                query += encodeURIComponent(name) + '=' + encodeURIComponent(value) + '&';
        }
        return query.length ? query.substr(0, query.length - 1) : query;
    };
    // Override $http service's default transformRequest
    $httpProvider.defaults.transformRequest = [function(data) {
        return angular.isObject(data) && String(data) !== '[object File]' ? param(data) : data;
    }];
});

参考

apache - 映射

Alias /imgSrc/ “/sdzw/ibp/upload/ptlogin/”
<Directory “/sdzw/ibp/upload/ptlogin/”>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride all
    Order allow,deny
    Allow from all
</ Directory>

阅读全文

PHP - 调用 webservice 接口 - php 5.4 以下

<?php
header('Content-Type:text/html; charset=utf-8');
require_once('nusoap.php');
$ip = $_REQUEST['ip'];
$func = $_REQUEST['func'];
$param = $_REQUEST['param'];
$namespace = $_REQUEST['namespace'];
$paras = json_decode(json_encode($param), true);
try{
    $client = new soapclient('http://'.$ip.'/xx/Webservice?wsdl', true);
    $client->soap_defencoding = 'UTF-8';
    $client->decode_utf8 = false;
    $client->xml_encoding = 'UTF-8';
    $result = $client->call($func, $paras, $namespace);
    print_r($result);
}catch(SoapFault $exception){
    print_r($exception);
}
?>

阅读全文

PHP - 调用 webservice 接口 - php 5.4 以上

requestWsdl.php

<?php
header('content-type:text/html;charset=utf-8');
$ip = $_RESQUEST['ip'];
$function = $_REQUEST['function'];
$param = $_REQUEST['param'];
$res = new WSDL();
$res->getData($ip, $function, $param);
class WSDL{
    public function __construct(){
    }
    public function getData($ip, $function, $param = array()){
        try {
            $client = new SoapClient('http://' . $ip . '/xxx/Webservice?wsdl',
array('encoding' => 'UTF-8'));
            $result = $client->{$function}($param);
            $result = get_object_vars($result);
            print_r($result);
        } catch (SoapFault $exception) {
            print_r($exception);
        }
    }
    public function getFunctions($ip){
        $client = new SoapClient('http://' . $ip . '/xxx/Webservice?wsdl', 
array('encoding' => 'UTF-8'));
        $functions = $client->__getFunctions();
        print_r($functions);
    }
    public function getTypes($ip){
        $client = new SoapClient('http://' . $ip . '/xxx/Webservice?wsdl', array('encoding' => 'UTF-8'));
        $types = $client->__getTypes();
        print_r($types);
    }
}

阅读全文

PHP - 递归无限级分类下的所有子集

<?php
/**
 * 递归无限级分类【先序遍历算】,获取任意节点下所有子孩子
 * @param array $arrCate 待排序的数组
 * @param int $parent_id 父级节点
 * @param int $level 层级数
 * @return array $arrTree 排序后的数组
 */
function getMenuTree($arrCat, $parent_id = 0, $level = 0)
{
    static  $arrTree = array(); //使用static代替global
    if( empty($arrCat)) {
        return FALSE;
    }
    $level++;
    foreach($arrCat as $key => $value)
    {
        if($value['parent_id' ] == $parent_id)
        {
            $value[ 'level'] = $level;
            $arrTree[] = $value;
            unset($arrCat[$key]); //注销当前节点数据,减少已无用的遍历
            getMenuTree($arrCat, $value[ 'id'], $level);
        }
    }
    return $arrTree;
}
/**
 * 测试数据
 */
$arrCate = array(  //待排序数组
    array( 'id'=>1, 'name' =>'顶级栏目一', 'parent_id'=>0),
    array( 'id'=>2, 'name' =>'顶级栏目二', 'parent_id'=>0),
    array( 'id'=>3, 'name' =>'栏目三', 'parent_id'=>1),
    array( 'id'=>4, 'name' =>'栏目四', 'parent_id'=>3),
    array( 'id'=>5, 'name' =>'栏目五', 'parent_id'=>4),
    array( 'id'=>6, 'name' =>'栏目六', 'parent_id'=>2)
);
header('Content-type:text/html; charset=utf-8'); //设置utf-8编码
echo '<pre>';
print_r(getMenuTree($arrCate, 0, 0));
echo '</pre>';
?>

阅读全文

PHP - JS - JSON

php array 轉 json

$json = json_encode($array);

js json 轉 string

var json;
var str = JSON.stringify(json);

解決 Linux 下 phpstorm 無法輸入中文

phpstorm.sh

# 省略原来的代码
CLASSPATH="$CLASSPATH:$IDE_HOME/lib/jna.jar"
if [ -n "$WEBIDE_CLASSPATH" ]; then
  CLASSPATH="$CLASSPATH:$WEBIDE_CLASSPATH"
fi
# ------需要加入的代码begin---------------------------------
XMODIFIERS="@im=fcitx"
export XMODIFIERS
# ------需要加入的代码end-----------------------------------
# ------一定要在 # Run the IDE. 的前面的任何地方-----
# ---------------------------------------------------------
# Run the IDE.
# ---------------------------------------------------------
# 省略原来的代码

PHP - 查询结果转字符串

function strQuery($sql){
    $sql = mysql_query($sql);
    $resule = array();
    while($row = mysql_fetch_assoc($sql)){
        $result[] = $row;
    }
    $str = "";
    foreach($result as $key => $val){
        $str = $str.implode("-", $val)."|"; 
    }
    $data = substr($str, 0, strlen($str)-1);
    return $data;
}