2012-01-03 51 views
0

我有这样的功能在这里:,卷曲,未能张贴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://输入流)与状态一起传递。有任何想法吗?

回答

0

发现问题。

我叫http://localhost/api,因为我认为它会自动加载index.php,然后我要改变文件夹的默认文件名。

问题是我没有在最后添加index.php - 我应该叫http://localhost/api/index.php。 这种方式它与cURL和fopen一起工作。

任何想法如何在不透露文件名的情况下调用API?

+0

把.htaccess文件放到你放置php的文件夹中。在htaccess中写下: RewriteEngine在 RewriteRule。* myfile.php [PT,L] 所有的请求都去myfile.php。 – tcak 2012-01-04 12:15:24

+0

似乎没有工作。我得到一个Moved Permanently 301响应,如果我将CURLOPT_FOLLOWLOCATION设置为true,我会像以前一样得到一个空的结果。 – donkapone 2012-01-04 14:54:55

+0

http://www.noupe.com/php/10-mod_rewrite-rules-you-should-know.html检查了这一点。可以帮助你正确地编写htaccess。也许你写错了。还有更多的事情,试着写一些东西,可能是日期时间,或者你发送的请求到php文件中的文件中。所以你可以理解它可以正常工作。 – tcak 2012-01-04 18:57:04

0

你能确定curl_exec函数成功结束吗?另外,你为什么不使用fopen来达到这个目的。我写了一个JSON RPC客户端和服务器。我正在用fopen发送请求,并且它工作的很好。

 $httpRequestOptions = 
      array(
       'http'=>array(
        'method'=>'POST', 
        'header'=>'Content-type: application/json', 
        'content'=>$requestJSON 
       ) 
      ); 

     $context = stream_context_create($httpRequestOptions); 

     // send request 
     if($fileHandler = @fopen($serverURL, 'r', false, $context)){ 

我不写剩下的。你可以使用我写的这段代码。

+0

是的,我敢肯定curl_exec成功结束,因为我回声一个json编码数组也包含状态整数,我得到该数组的一部分。我会尝试用fopen重写这个函数。 – donkapone 2012-01-03 21:49:13

+0

我用fopen重做它,但仍然没有运气得到file_get_contents('php:// input')的数据;即使它读取了状态。 – donkapone 2012-01-03 22:07:25

+1

只是为了确定它,你正在写一个URL到serverURL部分,对吧?而不是文件路径。它一定是像'http:// localhost/server.php'。如果你写'server.php'或类似的东西,它不起作用。发送的参数不会传递给'php://输入' – tcak 2012-01-04 08:44:40