从 PHP url 返回数组

问题描述 投票:0回答:4

我不知道这是否可能

我在两个网站上工作,我想通过 URL 将数组从一个站点传递到另一个站点

例如

$myreceivearray = http://mydomain/myscript.php

这个 myscript.php 应该返回一个数组。

再次在问题中添加一些部分

根据我试过的答案,我的 myscript.php 代码是

<?php
if (session_id() == "") session_start(); // Initialize Session data
ob_start(); // Turn on output buffering
?>
<?php include_once "ewcfg10.php" ?>
<?php include_once "adodb5/adodb.inc.php" ?>
<?php include_once "phpfn10.php" ?>
<?php include_once "ACCOUNTinfo.php" ?>
<?php include_once "userfn10.php" ?>
<?php

//
// Page class
//

$custompage = NULL; // Initialize page object first

class ccustompage {

    // Page ID
    var $PageID = 'custompage';

    // Project ID
    var $ProjectID = "{D5B1B389-2DCD-4664-BAD5-27C1596B8577}";

    // Page object name
    var $PageObjName = 'custompage';

    // Page name
    function PageName() {
        return ew_CurrentPage();
    }

    // Page URL
    function PageUrl() {
        $PageUrl = ew_CurrentPage() . "?";
        return $PageUrl;
    }

    // Message
    function getMessage() {
        return @$_SESSION[EW_SESSION_MESSAGE];
    }

    function setMessage($v) {
        ew_AddMessage($_SESSION[EW_SESSION_MESSAGE], $v);
    }

    function getFailureMessage() {
        return @$_SESSION[EW_SESSION_FAILURE_MESSAGE];
    }

    function setFailureMessage($v) {
        ew_AddMessage($_SESSION[EW_SESSION_FAILURE_MESSAGE], $v);
    }

    function getSuccessMessage() {
        return @$_SESSION[EW_SESSION_SUCCESS_MESSAGE];
    }

    function setSuccessMessage($v) {
        ew_AddMessage($_SESSION[EW_SESSION_SUCCESS_MESSAGE], $v);
    }

    function getWarningMessage() {
        return @$_SESSION[EW_SESSION_WARNING_MESSAGE];
    }

    function setWarningMessage($v) {
        ew_AddMessage($_SESSION[EW_SESSION_WARNING_MESSAGE], $v);
    }

    // Show message
    function ShowMessage() {
        $hidden = FALSE;
        $html = "";

        // Message
        $sMessage = $this->getMessage();
        $this->Message_Showing($sMessage, "");
        if ($sMessage <> "") { // Message in Session, display
            if (!$hidden)
                $sMessage = "<button type=\"button\" class=\"close\" data-dismiss=\"alert\">&times;</button>" . $sMessage;
            $html .= "<div class=\"alert alert-success ewSuccess\">" . $sMessage . "</div>";
            $_SESSION[EW_SESSION_MESSAGE] = ""; // Clear message in Session
        }

        // Warning message
        $sWarningMessage = $this->getWarningMessage();
        $this->Message_Showing($sWarningMessage, "warning");
        if ($sWarningMessage <> "") { // Message in Session, display
            if (!$hidden)
                $sWarningMessage = "<button type=\"button\" class=\"close\" data-dismiss=\"alert\">&times;</button>" . $sWarningMessage;
            $html .= "<div class=\"alert alert-warning ewWarning\">" . $sWarningMessage . "</div>";
            $_SESSION[EW_SESSION_WARNING_MESSAGE] = ""; // Clear message in Session
        }

        // Success message
        $sSuccessMessage = $this->getSuccessMessage();
        $this->Message_Showing($sSuccessMessage, "success");
        if ($sSuccessMessage <> "") { // Message in Session, display
            if (!$hidden)
                $sSuccessMessage = "<button type=\"button\" class=\"close\" data-dismiss=\"alert\">&times;</button>" . $sSuccessMessage;
            $html .= "<div class=\"alert alert-success ewSuccess\">" . $sSuccessMessage . "</div>";
            $_SESSION[EW_SESSION_SUCCESS_MESSAGE] = ""; // Clear message in Session
        }

        // Failure message
        $sErrorMessage = $this->getFailureMessage();
        $this->Message_Showing($sErrorMessage, "failure");
        if ($sErrorMessage <> "") { // Message in Session, display
            if (!$hidden)
                $sErrorMessage = "<button type=\"button\" class=\"close\" data-dismiss=\"alert\">&times;</button>" . $sErrorMessage;
            $html .= "<div class=\"alert alert-error ewError\">" . $sErrorMessage . "</div>";
            $_SESSION[EW_SESSION_FAILURE_MESSAGE] = ""; // Clear message in Session
        }
        echo "<table class=\"ewStdTable\"><tr><td><div class=\"ewMessageDialog\"" . (($hidden) ? " style=\"display: none;\"" : "") . ">" . $html . "</div></td></tr></table>";
    }

    //
    // Page class constructor
    //
    function __construct() {
        global $conn, $Language, $UserAgent;

        // User agent
        $UserAgent = ew_UserAgent();
        $GLOBALS["Page"] = &$this;

        // Language object
        if (!isset($Language)) $Language = new cLanguage();

        // User table object (ACCOUNT)
        if (!isset($GLOBALS["ACCOUNT"])) $GLOBALS["ACCOUNT"] = new cACCOUNT;

        // Page ID
        if (!defined("EW_PAGE_ID"))
            define("EW_PAGE_ID", 'custompage', TRUE);

        // Start timer
        if (!isset($GLOBALS["gTimer"])) $GLOBALS["gTimer"] = new cTimer();

        // Open connection
        if (!isset($conn)) $conn = ew_Connect();
    }

    // 
    //  Page_Init
    //
    function Page_Init() {
        global $gsExport, $gsExportFile, $UserProfile, $Language, $Security, $objForm;

        // Security
        $Security = new cAdvancedSecurity();
        if (!$Security->IsLoggedIn()) $Security->AutoLogin();

        // Uncomment codes below for security
        //if (!$Security->IsLoggedIn())
        //  $this->Page_Terminate("login.php");

        if (@$_GET["export"] <> "")
            $gsExport = $_GET["export"]; // Get export parameter, used in header
        $this->CurrentAction = (@$_GET["a"] <> "") ? $_GET["a"] : @$_POST["a_list"]; // Set up curent action
        global $gbOldSkipHeaderFooter, $gbSkipHeaderFooter;
        $gbOldSkipHeaderFooter = $gbSkipHeaderFooter;
        $gbSkipHeaderFooter = TRUE;

        // Global Page Loading event (in userfn*.php)
        Page_Loading();

        // Page Load event
        $this->Page_Load();
    }

    //
    // Page_Terminate
    //
    function Page_Terminate($url = "") {
        global $conn;
        global $gbOldSkipHeaderFooter, $gbSkipHeaderFooter;
        $gbSkipHeaderFooter = $gbOldSkipHeaderFooter;

        // Page Unload event
        $this->Page_Unload();

        // Global Page Unloaded event (in userfn*.php)
        Page_Unloaded();
        $this->Page_Redirecting($url);

         // Close connection
        $conn->Close();

        // Go to URL if specified
        if ($url <> "") {
            if (!EW_DEBUG_ENABLED && ob_get_length())
                ob_end_clean();
            header("Location: " . $url);
        }
    }

    //
    // Page main
    //
    function Page_Main() {
        global $Security, $Language;

        //$this->setSuccessMessage("Welcome " . CurrentUserName());
        // Put your custom codes here

    }

    // Page Load event
    function Page_Load() {

        //echo "Page Load";
    }

    // Page Unload event
    function Page_Unload() {

        //echo "Page Unload";
    }

    // Page Redirecting event
    function Page_Redirecting(&$url) {

        // Example:
        //$url = "your URL";

    }

    // Message Showing event
    // $type = ''|'success'|'failure'
    function Message_Showing(&$msg, $type) {

        // Example:
        //if ($type == 'success') $msg = "your success message";

    }
}
?>
<?php ew_Header(FALSE) ?>
<?php

// Create page object
if (!isset($custompage)) $custompage = new ccustompage();

// Page init
$custompage->Page_Init();

// Page main
$custompage->Page_Main();
?>
<?php include_once "header.php" ?>
<?php
$custompage->ShowMessage();
?>
<!-- Put your custom html here -->
<?PHP
$myarray = array(1,2,3,4,5);
echo json_encode($myarray);
?>

<?php include_once "footer.php" ?><?php
$custompage->Page_Terminate();
?>

我不能更改所有代码,因为我必须集成到我的旧项目中。在最后 7 行中,我输入了我的代码,当我打开此页面时,它仅输出为 [1,2,3,4,5] 当我试着把它写成

<?PHP
$myreceivearray = json_decode(file_get_contents('http://mydomain/blankpage.php'));
print_r($myreceivearray);
?>

我这里没拿到数组

请帮助我。

php arrays url
4个回答
1
投票

你可以用 JSON 做到这一点。使用,myscript.php 中的方法

json_encode($yourarray) 

使用 JSON ( http://www.json.org ) 对数组进行编码。在另一个站点上,您可以使用

$myreceivearray = json_decode(file_get_contents("http://mydomain/myscript.php"));

0
投票

http://php.net/manual/en/function.file-get-contents.php

您可以使用

file_get_contents

将 url 的结果读取为字符串
$myreceivearray = file_get_contents(http://mydomain/myscript.php);

您将希望 myscript.php 以某种可以解析为数组

json_decode
或类似内容的格式回显数组。


0
投票

对于编码:

echo json_encode($arr);

用于解码:

$arr = json_decode(file_get_contents('http://mydomain/myscript.php'), true);

不要忘记设置

json_decode
的第二个参数。


0
投票

您需要在第一个脚本中对数组进行编码并从另一个脚本中检索它。您可以将其编码为 JSON。我还建议相应地设置标题:

myscript.php

// encode response
$response = json_encode($values);

// set headers
header('Content-Type: application/json');
header('Content-Encoding: UTF-8');
header('Content-Length: ' . strlen($response));
header('Pragma: no-cache');
header('Expires: 0');

// send value
print $response;
exit();

从其他脚本中获取此内容并对其进行解码:

另一个剧本

$response = file_get_contents('http://mydomain/myscript.php');
$values = json_decode($response);
© www.soinside.com 2019 - 2024. All rights reserved.