免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2419 | 回复: 4
打印 上一主题 下一主题

curl老出错.服务器端的确收到了客户端发过来的信息,但是客户观确收不到. [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-11-03 17:03 |只看该作者 |倒序浏览
class CurlPRC_Class
{
    var $ch; // curl handle

    function CurlPRC_Class()
    {
        $this->ch = null;
        $this->ch = CurlPRC_Class::create();
        return true;
    }

    function __destruct()
    {
        if ($this->ch != null)
        {
            curl_close($this->ch);
            $this->ch = null;
        }
        return true;
    }
    function create()   // init handle
    {
        $ch = null;
        if (!function_exists('curl_init'))
        {
            return false;
        }
        $ch = curl_init();
        if (!is_resource($ch))
        {
            return false;
        }
        return $ch;
    }

    function call($url, $data)  // rpc call
    {
        if (!is_array($data)) return false;

        $post_args = json_encode($data);
         // set
        curl_setopt($this->ch, CURLOPT_TIMEOUT, 3);
        curl_setopt($this->ch, CURLOPT_URL, $url);
        curl_setopt($this->ch, CURLOPT_HEADER, false);
        curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($this->ch, CURLOPT_FORBID_REUSE, false);
        // curl_setopt($ch, CURLOPT_PROXY, "192.168.1.1");
        // curl_setopt($ch, CURLOPT_PROXYPORT, 8080);
        curl_setopt($this->ch, CURLOPT_POST, 1);
        curl_setopt($this->ch, CURLOPT_POSTFIELDS, "post=$post_args");

        // call
        echo "........1B\n";
        $post_ret = curl_exec($this->ch);
        echo "........2B\n";

        if (curl_errno($this->ch))
        {
            return false;
        }

        $call_ret = json_decode($post_ret, true);
        if ($call_ret['return'] == "true")
        {
            return $call_ret;
        }
        return false;
    }
}



<?php
    $rpc = new CurlPRC_Class();

     $update_addr = "http://192.168.0.10/rpc_update.php";

    $post_data = array();
    $post_data["devid"]='SO6J0K7K';
  
    echo "........1A\n";
    $ret = $rpc->call($update_addr, $post_data);
    echo "........2A\n";
    if ($ret == false)
    {
        echo "rpc call error <br>\n";
        exit;
    }

?>

谁帮看看哪出错了,
老出现rpc call error
但是服务器端的确收到了客户端发过来的信息,我已试验过)

论坛徽章:
0
2 [报告]
发表于 2008-11-03 17:41 |只看该作者
if ($call_ret['return'] == "true")
        {
            return $call_ret;
        }

改成这样的:
if ($call_ret['return'] == true)
        {
            return $call_ret;
        }

论坛徽章:
0
3 [报告]
发表于 2008-11-03 19:07 |只看该作者
还是出错.

论坛徽章:
0
4 [报告]
发表于 2008-11-03 21:08 |只看该作者

回复 #3 greenbox 的帖子

我想问一下你这里的$call_ret['return']  有值吗?你先看看$call_ret 里面的值

论坛徽章:
0
5 [报告]
发表于 2008-11-06 16:27 |只看该作者

  1. call里面改了,
  2. 发送时不用json code,就行了,但是接收时用json code就行,不太明白

  3.   function call($url, $data)  // rpc call
  4.     {
  5.         if (!is_array($data)) return false;

  6.         $post_args = json_encode($data);
  7.         echo "url:$url\n";

  8.         //curl_setopt($this->ch, CURLOPT_FORBID_REUSE, false);
  9.         //curl_setopt($this->ch, CURLOPT_TIMEOUT, 3);
  10.         //curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, 1);
  11.         curl_setopt($this->ch, CURLOPT_URL, $url);
  12.         curl_setopt($this->ch, CURLOPT_HEADER, false);
  13.         curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true);
  14.         curl_setopt($this->ch, CURLOPT_POST, 1);
  15.         curl_setopt($this->ch, CURLOPT_POSTFIELDS, $data);

  16.         // call
  17.         $post_ret = curl_exec($this->ch);
  18.         $errno = curl_errno($this->ch);
  19.         if ($errno)
  20.         {
  21.             echo "call errno:$errno\n";
  22.             return false;
  23.         }
  24.         $call_ret = json_decode($post_ret, true);
  25.         if ($call_ret['return'] == "true")
  26.         {
  27.             echo "\n\n=============res =============\n $post_ret\n==========================\n\n";
  28.             return $call_ret;
  29.         }
  30.         return false;
  31.     }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP