请求标题CURLOPT_HEADER时未返回任何帖子数据。 PHP CURL [英] no post data returned when requesting headers CURLOPT_HEADER. PHP CURL

查看:349
本文介绍了请求标题CURLOPT_HEADER时未返回任何帖子数据。 PHP CURL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用curl和PHP发送一个POST请求,以JSON对象的形式发送一个用户名/密码数组。



到目前为止,所有的工作都很奇妙。现在我想读头,这样我可以解析cookie。我知道我可以使用一个文件/ jar并已经测试 - 工作得很好。我想不写一个东西到磁盘。



所以我标记为CURLOPT_HEADER为TRUE,我可以解析cookie值。



但是,返回的数据已删除。完全消失...
当我标记_HEADER FALSE - 我看到响应。



代码:

  $ login_array = array('login'=> array('username'=> $ username,'password'=> $ password) 
$ login_json = json_encode($ login_array);

$ ch = curl_init();

curl_setopt($ ch,CURLOPT_VERBOSE,TRUE);
curl_setopt($ ch,CURLOPT_POST,TRUE);
curl_setopt($ ch,CURLOPT_HEADER,TRUE); < - this line f *** s it up up ...
curl_setopt($ ch,CURLOPT_RETURNTRANSFER,TRUE);
curl_setopt($ ch,CURLOPT_POSTFIELDS,$ login_json);
curl_setopt($ ch,CURLOPT_HTTPHEADER,array(Content-Type:application / json));
curl_setopt($ ch,CURLOPT_URL,'http:// localhost / test / token');

$ result = curl_exec($ ch);

$ http_code = curl_getinfo($ ch,CURLINFO_HTTP_CODE);
if($ http_code!= 200){/ * do something * /}

preg_match_all('| Set-Cookie:(。*); | U',$ result,$ content );
$ cookies = implode(';',$ content [1]);
echocookie:$ cookies< br>; &_-感谢_HEADER的cookie值

$ response = json_decode($ result);
$ user_id = $ response-> user-> id;
echo'user_id:' $ user_id。 '< br>'; < - 空的男人...标题是关闭的身体是关闭...

我们一直在挖掘不同的帖子,似乎opts的顺序有所不同。

解决方案

使用:

  $ body = mb_substr($ result,curl_getinfo($ ch,CURLINFO_HEADER_SIZE)); 
$ response = json_decode($ body);
$ user_id = $ response-> user-> id;
echo'user_id:'。 $ user_id。 '< br>';


i am making a POST request using curl and PHP, sending a username/password array as a JSON object.

so far all is working wonderfully. now i wanted to read the headers so i can parse the cookie. i know i can use a file/jar and have tested that - works well. i want to not write a thing to disk.

so i flagged CURLOPT_HEADER as TRUE and i can parse the cookie value.

HOWEVER - the returned data is gone. totally gone... when i flag _HEADER FALSE - i see the response.

the code:

    $login_array = array('login' => array('username' => $username, 'password' => $password));
    $login_json = json_encode($login_array);

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
    curl_setopt($ch, CURLOPT_POST, TRUE);
    curl_setopt($ch, CURLOPT_HEADER, TRUE); <-- this line f***s it all up...
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $login_json);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json")); 
    curl_setopt($ch, CURLOPT_URL, 'http://localhost/test/token');

    $result = curl_exec($ch);

    $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    if ($http_code != 200) { /* do something */ }

    preg_match_all('|Set-Cookie: (.*);|U', $result, $content);    
    $cookies = implode(';', $content[1]);
    echo "cookie: $cookies<br>"; <-- thanks _HEADER for the cookie value

    $response = json_decode($result);
    $user_id = $response->user->id;
    echo 'user_id: ' . $user_id . '<br>'; <-- empty man... headers are on means body is off...

i've been digging through different posts and it seems the order of the opts makes a difference. tried moving them about - no luck.

thoughts?!

解决方案

Use:

$body=mb_substr($result, curl_getinfo($ch,CURLINFO_HEADER_SIZE));
$response = json_decode($body);
$user_id = $response->user->id;
echo 'user_id: ' . $user_id . '<br>';

这篇关于请求标题CURLOPT_HEADER时未返回任何帖子数据。 PHP CURL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆