2014-06-13 40 views
0

我最近更改了我的虚拟主机,不幸的是管理员已禁用了file_get_content和其他一些功能。替代file_get_content(php://输入)

该脚本的目的是直接从设备获取iOS设备的信息。之前,我就是用这个代码可以这样做:

$content = file_get_contents('php://input'); 
    file_put_contents("data.txt", $content); 

,结果是这样的(在data.txt中)

<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> 
<plist version="1.0"> 
<dict> 
    <key>IMEI</key> 
    <string>01 257233 621346 0</string> 
    <key>PRODUCT</key> 
    <string>iPhone2,1</string> 
    <key>SERIAL</key> 
    <string>5K1124GHEDG</string> 
    <key>UDID</key> 
    <string>07cc825055gftr4edsa3f06d0373376a7664a66a</string> 
    <key>VERSION</key> 
    <string>10B329</string> 
</dict> 
</plist> 

由于卷曲功能,我的服务器上启用了,我想这代码以某种不同的方式,但没有奏效。 data.txt因此被清空!

$ch = curl_init(); 
    $timeout = 0; 
    curl_setopt ($ch, CURLOPT_URL, "php://input"); 
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout); 
    $contents = curl_exec($ch); 
    curl_close($ch); 
    file_put_contents("data.txt", $contents); 

更新:

让我解释一下它是如何工作的。我们将用户导向存储在我们服务器上的配置文件(例如device.mobileconfig)。一旦用户点击,他们将被要求安装。其内容是:

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> 
<plist version="1.0"> 
<dict> 
    <key>PayloadContent</key> 
    <dict> 
     <key>URL</key> 
     <string>http://mywebsite.com/get.php</string> 
     <key>DeviceAttributes</key> 
      <array> 
       <string>UDID</string> 
       <string>VERSION</string> 
       <string>PRODUCT</string> 
       <string>SERIAL</string> 
      </array> 
    </dict> 
    <key>PayloadDescription</key> 
    <string>Install to get device info</string> 
    <key>PayloadDisplayName</key> 
    <string>UDID Finder</string> 
    <key>PayloadIdentifier</key> 
    <string>com.myud.ir</string> 
    <key>PayloadOrganization</key> 
    <string>MyUD.ir</string> 
    <key>PayloadRemovalDisallowed</key> 
    <false/> 
    <key>PayloadType</key> 
    <string>Profile Service</string> 
    <key>PayloadUUID</key> 
    <string>57A223FD-F675-90B1-8F43-22D3FC3BC181</string> 
    <key>PayloadVersion</key> 
    <integer>1</integer> 
</dict> 
</plist> 

该配置文件运行get.php从设备中获取数据。我试过克里斯蒂安的一个,但没有工作。我可能在某个地方有一个错误。 get.php是:

<?php 
    $xml = new SimpleXMLElement('<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"><plist version="1.0"/>'); 
    $dict = $xml->addChild('dict'); 
    foreach ($_POST as $item => $value) { 
     $dict->addChild('string', $item); 
     $dict->addChild('key', $value); 
    } 
    file_put_contents("data.txt", trim(str_replace('<?xml version="1.0"?>', '', $xml->asXML()))); 
?> 

P.S.

我只需要

<dict></dict> 
+0

还有一个curl选项来设置目标文件。 – Charles

+0

curl对php的流不了解。它期望一个合适的网址,并且会正确地抱怨'php'不是一个已知的协议。 –

+0

@MarcB:那你的意思是我不能通过Curt方法获取数据? – PersianHero

回答

0

非常感谢你克里斯蒂安。有效。

我还发现了PEAR php组件的另一种方式,它提供了很多功能,这些功能可以替代像我这样挣扎的人的主要PHP功能!

例如,在我的情况,我上传的file_get_content功能到同一文件夹,其中包括的是,在get.php

代替

$content = file_get_contents('php://input'); 
file_put_contents("data.txt", $content); 

我现在使用

require_once 'file_get_contents.php'; 
$content = php_compat_file_get_contents('php://input'); 
file_put_contents("data.txt", $content); 
+0

优秀的,不客气! – Cristian

0

之间的数据假设php://input是一个只读流,可以让你请求主体读取原始数据,可以通过获取$_POST数据解决这个问题,然后创建一个xml文件使用php的SimpleXMLElement职业女巫然后将写入data.txt。 您可以用这种方式进行:

$xml = new SimpleXMLElement('<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"><plist version="1.0"/>'); 
$dict = $xml->addChild('dict'); 
foreach ($_POST as $item => $value) { 
    $dict->addChild('string', $item); 
    $dict->addChild('key', $value); 
} 
file_put_contents("data.txt", trim(str_replace('<?xml version="1.0"?>', '', $xml->asXML()))); 

另一种解决方案:

$f = fopen('php://input','r'); 
$out = fopen('data.txt','w'); 
$body = ""; 
while($line = fgets($f)) { 
    $body .= $line; 
} 
fwrite($out, $body); 
fclose($f); 
fclose($out); 
+0

没有工作。请检阅我的第一篇文章。我添加了更多解释 – PersianHero

+0

您使用$ _POST将数据传递给您的php脚本?或$ _GET? – Cristian

+0

我什么都不做。一旦用户点击“安装”,iOS与get.php建立连接,并通过$ _POST方法发送我们在配置文件中定义的所需信息(在我们的例子中是UDUD,VERSION,PRODUCT,SERIAL)。 我记录了一段短片的流程。 https://www.youtube.com/watch?v=qOn-sAPnMew – PersianHero