search

Home  >  Q&A  >  body text

求大神指导, php中 curl模拟post发送json并接收json,post数据不成功,也取不回json值

代码放在本地做测试,一个简单的登录,但是curl post不过去数据,也取不回这个success

1.png

1.png

然后curl代码如下:

<?php
header("content-Type: text/html; charset=UTF-8");$login_url="http://127.0.0.3/35/doLogin.php";$data = array("username" => "demo", "password" => "demo","is_ajax"=>"1");                                                                    
$data_string = json_encode($data);//提交登录表单请求$ch=curl_init($login_url);
curl_setopt($ch,CURLOPT_HEADER,0);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,0);
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$data_string);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("X-Requested-With: XMLHttpRequest"));  
$data=curl_exec($ch);
curl_close($ch);

var_dump(json_decode($data,true));  
?>

访问curl代码打印出来是NULL 

1.png

高洛峰高洛峰2951 days ago781

reply all(1)I'll reply

  • 三叔

    三叔2016-11-03 10:17:41

    $ch        = curl_init();        
    $timeout   = 3;        
    $useragent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)";        
    $header    = array(                
                    'Accept-Language: zh-cn', 
                    'Connection: Keep-Alive', 
                    'Cache-Control: no-cache', 
                    'Content-Type: Application/json;charset=utf-8',
                    "X-Requested-With: XMLHttpRequest"
                );
    $login_url="http://127.0.0.3/35/doLogin.php";
    $data = array("username" => "demo", "password" => "demo","is_ajax"=>"1");                                                                    
    $json = json_encode($data);            
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);        
    curl_setopt($ch, CURLOPT_URL, $login_url);
    curl_setopt($ch, CURLOPT_POST, TRUE);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $json);                        
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);            
    $res = curl_exec($ch);        
    echo '
    ';print_r(json_decode($res,true));


    reply
    0
  • Cancelreply