我有这样的功能在这里:,卷曲,未能张贴JSON数据
public static function call($action, array $args)
{
$post_args = array(
'action' => $action,
'args' => $args
);
$stream = json_encode($post_args);
$headers = array(
'Content-type: application/json',
'Accept: application/json',
'Expect:'
);
$userpwd = self::$_user.':'.sha1(sha1(self::$_pass));
$ch = curl_init();
$args = array(
CURLOPT_URL => self::$_url,
CURLOPT_FOLLOWLOCATION => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_POST => TRUE,
CURLOPT_POSTFIELDS => $stream,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_USERPWD => $userpwd
);
curl_setopt_array($ch, $args);
$res = curl_exec($ch);
$data = json_decode($res, true);
if (isset($data) === TRUE
&& empty($data) === FALSE
) {
$res = $data;
}
return $res;
}//end call()
,并在那里我张贴的网址,我只是在做:
echo file_get_contents('php://input');
,但是却一无所获,即使我发布数据。可能是什么问题呢?我处于死胡同。
此外,为什么我只需将CURLOPT_FOLLOWLOCATION设置为TRUE即可在我的本地计算机上发布简单的虚拟主机URL,而不执行任何重定向。
编辑:
试图用的fopen重做像这样:
public static function call($action, array $args)
{
$post_args = array(
'action' => $action,
'args' => $args
);
$stream = json_encode($post_args);
$userpwd = self::$_user.':'.sha1(sha1(self::$_pass));
$opts = array(
'http' => array(
'method' => 'POST',
'header' => array(
"Authorization: Basic ".base64_encode($userpwd),
"Content-type: application/json"
),
'content' => $stream
)
);
$context = stream_context_create($opts);
$res = '';
$fp = fopen(self::$_url, 'r', false, $context);
if($fp){
while (!feof($fp)){
$res .= fread($fp, 128);
}
}
return $res;
}//end call()
没有成功。该连接与curl和fopen一起工作,因为我将结果(仅仅是php://输入流)与状态一起传递。有任何想法吗?
把.htaccess文件放到你放置php的文件夹中。在htaccess中写下: RewriteEngine在 RewriteRule。* myfile.php [PT,L] 所有的请求都去myfile.php。 – tcak 2012-01-04 12:15:24
似乎没有工作。我得到一个Moved Permanently 301响应,如果我将CURLOPT_FOLLOWLOCATION设置为true,我会像以前一样得到一个空的结果。 – donkapone 2012-01-04 14:54:55
http://www.noupe.com/php/10-mod_rewrite-rules-you-should-know.html检查了这一点。可以帮助你正确地编写htaccess。也许你写错了。还有更多的事情,试着写一些东西,可能是日期时间,或者你发送的请求到php文件中的文件中。所以你可以理解它可以正常工作。 – tcak 2012-01-04 18:57:04