Use cookie saved by curl in stream_get_contents











up vote
0
down vote

favorite












I wanted to send POST request , Actually curl is 100ms slower than stream_get_contents, so i wanted to use the latter.



I have a cookie.txt saved by curl , how do i use that to my stream_get_contents function below.



function poster($url, $data, $optional_headers = null)
{
$params = array('http' => array(
'method' => 'POST',
'content' => $data
));
if ($optional_headers !== null) {
$params['http']['header'] = $optional_headers;
}
$ctx = stream_context_create($params);
$fp = @fopen($url, 'rb', false, $ctx);
if (!$fp) {
throw new Exception("Problem with $url, $php_errormsg");
}
$response = @stream_get_contents($fp);
if ($response === false) {
throw new Exception("Problem reading data from $url, $php_errormsg");
}
return $response;
}


My cookie.txt stored by curl looks like below



secure.domain.com   FALSE   /   FALSE   0   InterSecure AyDzUp5AEKz0ErJYeJF2221lIA$$
#HttpOnly_secure.domain.com FALSE / TRUE 0 ASP.NET_SessionId eqyrgo545czouimlnqc223f0qyi


I tried something like



 $header = array(
'Referer: https://secure.domain.com/IDirectTrading/customer/login.aspx',
'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.20 Safari/537.36',
'Cookie : InterSecure = AyDzUp5AEKz0ErJYeJF2221lIA$$;ASP.NET_SessionId = eqyrgo545czouimlnqc223f0qyi'
);
$CE = poster("https://secure.domain.com/Handler.ashx",$str,$header);


But cookie doesnt seems to work , Is there any way to parse cookie text file into array and use that into header ?










share|improve this question
























  • "doesn't seem to work" doesn't give us a lot to go on. Does the extra header get sent at all with the request? Have you tried getting rid of all the extra whitespace in that line?
    – miken32
    Nov 7 at 19:03










  • doesn't seem to work - The page i request is logged out , cookie doesnt seem to work.. whereas if i send curl request with that cookie file , response is logged in
    – Gracie williams
    Nov 7 at 19:04










  • Ok, but again, "logged out" doesn't mean anything in the context of us looking at this code. Have you verified that the cookie header is getting sent with the request?
    – miken32
    Nov 7 at 19:10










  • how do i check if its sent ?
    – Gracie williams
    Nov 7 at 19:11










  • Hmm, not sure actually, if you don't have access to the server. Did you try formatting the cookie properly per my answer?
    – miken32
    Nov 7 at 19:15















up vote
0
down vote

favorite












I wanted to send POST request , Actually curl is 100ms slower than stream_get_contents, so i wanted to use the latter.



I have a cookie.txt saved by curl , how do i use that to my stream_get_contents function below.



function poster($url, $data, $optional_headers = null)
{
$params = array('http' => array(
'method' => 'POST',
'content' => $data
));
if ($optional_headers !== null) {
$params['http']['header'] = $optional_headers;
}
$ctx = stream_context_create($params);
$fp = @fopen($url, 'rb', false, $ctx);
if (!$fp) {
throw new Exception("Problem with $url, $php_errormsg");
}
$response = @stream_get_contents($fp);
if ($response === false) {
throw new Exception("Problem reading data from $url, $php_errormsg");
}
return $response;
}


My cookie.txt stored by curl looks like below



secure.domain.com   FALSE   /   FALSE   0   InterSecure AyDzUp5AEKz0ErJYeJF2221lIA$$
#HttpOnly_secure.domain.com FALSE / TRUE 0 ASP.NET_SessionId eqyrgo545czouimlnqc223f0qyi


I tried something like



 $header = array(
'Referer: https://secure.domain.com/IDirectTrading/customer/login.aspx',
'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.20 Safari/537.36',
'Cookie : InterSecure = AyDzUp5AEKz0ErJYeJF2221lIA$$;ASP.NET_SessionId = eqyrgo545czouimlnqc223f0qyi'
);
$CE = poster("https://secure.domain.com/Handler.ashx",$str,$header);


But cookie doesnt seems to work , Is there any way to parse cookie text file into array and use that into header ?










share|improve this question
























  • "doesn't seem to work" doesn't give us a lot to go on. Does the extra header get sent at all with the request? Have you tried getting rid of all the extra whitespace in that line?
    – miken32
    Nov 7 at 19:03










  • doesn't seem to work - The page i request is logged out , cookie doesnt seem to work.. whereas if i send curl request with that cookie file , response is logged in
    – Gracie williams
    Nov 7 at 19:04










  • Ok, but again, "logged out" doesn't mean anything in the context of us looking at this code. Have you verified that the cookie header is getting sent with the request?
    – miken32
    Nov 7 at 19:10










  • how do i check if its sent ?
    – Gracie williams
    Nov 7 at 19:11










  • Hmm, not sure actually, if you don't have access to the server. Did you try formatting the cookie properly per my answer?
    – miken32
    Nov 7 at 19:15













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I wanted to send POST request , Actually curl is 100ms slower than stream_get_contents, so i wanted to use the latter.



I have a cookie.txt saved by curl , how do i use that to my stream_get_contents function below.



function poster($url, $data, $optional_headers = null)
{
$params = array('http' => array(
'method' => 'POST',
'content' => $data
));
if ($optional_headers !== null) {
$params['http']['header'] = $optional_headers;
}
$ctx = stream_context_create($params);
$fp = @fopen($url, 'rb', false, $ctx);
if (!$fp) {
throw new Exception("Problem with $url, $php_errormsg");
}
$response = @stream_get_contents($fp);
if ($response === false) {
throw new Exception("Problem reading data from $url, $php_errormsg");
}
return $response;
}


My cookie.txt stored by curl looks like below



secure.domain.com   FALSE   /   FALSE   0   InterSecure AyDzUp5AEKz0ErJYeJF2221lIA$$
#HttpOnly_secure.domain.com FALSE / TRUE 0 ASP.NET_SessionId eqyrgo545czouimlnqc223f0qyi


I tried something like



 $header = array(
'Referer: https://secure.domain.com/IDirectTrading/customer/login.aspx',
'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.20 Safari/537.36',
'Cookie : InterSecure = AyDzUp5AEKz0ErJYeJF2221lIA$$;ASP.NET_SessionId = eqyrgo545czouimlnqc223f0qyi'
);
$CE = poster("https://secure.domain.com/Handler.ashx",$str,$header);


But cookie doesnt seems to work , Is there any way to parse cookie text file into array and use that into header ?










share|improve this question















I wanted to send POST request , Actually curl is 100ms slower than stream_get_contents, so i wanted to use the latter.



I have a cookie.txt saved by curl , how do i use that to my stream_get_contents function below.



function poster($url, $data, $optional_headers = null)
{
$params = array('http' => array(
'method' => 'POST',
'content' => $data
));
if ($optional_headers !== null) {
$params['http']['header'] = $optional_headers;
}
$ctx = stream_context_create($params);
$fp = @fopen($url, 'rb', false, $ctx);
if (!$fp) {
throw new Exception("Problem with $url, $php_errormsg");
}
$response = @stream_get_contents($fp);
if ($response === false) {
throw new Exception("Problem reading data from $url, $php_errormsg");
}
return $response;
}


My cookie.txt stored by curl looks like below



secure.domain.com   FALSE   /   FALSE   0   InterSecure AyDzUp5AEKz0ErJYeJF2221lIA$$
#HttpOnly_secure.domain.com FALSE / TRUE 0 ASP.NET_SessionId eqyrgo545czouimlnqc223f0qyi


I tried something like



 $header = array(
'Referer: https://secure.domain.com/IDirectTrading/customer/login.aspx',
'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.20 Safari/537.36',
'Cookie : InterSecure = AyDzUp5AEKz0ErJYeJF2221lIA$$;ASP.NET_SessionId = eqyrgo545czouimlnqc223f0qyi'
);
$CE = poster("https://secure.domain.com/Handler.ashx",$str,$header);


But cookie doesnt seems to work , Is there any way to parse cookie text file into array and use that into header ?







php curl file-get-contents






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 7 at 19:19









miken32

22.7k84671




22.7k84671










asked Nov 7 at 18:51









Gracie williams

558




558












  • "doesn't seem to work" doesn't give us a lot to go on. Does the extra header get sent at all with the request? Have you tried getting rid of all the extra whitespace in that line?
    – miken32
    Nov 7 at 19:03










  • doesn't seem to work - The page i request is logged out , cookie doesnt seem to work.. whereas if i send curl request with that cookie file , response is logged in
    – Gracie williams
    Nov 7 at 19:04










  • Ok, but again, "logged out" doesn't mean anything in the context of us looking at this code. Have you verified that the cookie header is getting sent with the request?
    – miken32
    Nov 7 at 19:10










  • how do i check if its sent ?
    – Gracie williams
    Nov 7 at 19:11










  • Hmm, not sure actually, if you don't have access to the server. Did you try formatting the cookie properly per my answer?
    – miken32
    Nov 7 at 19:15


















  • "doesn't seem to work" doesn't give us a lot to go on. Does the extra header get sent at all with the request? Have you tried getting rid of all the extra whitespace in that line?
    – miken32
    Nov 7 at 19:03










  • doesn't seem to work - The page i request is logged out , cookie doesnt seem to work.. whereas if i send curl request with that cookie file , response is logged in
    – Gracie williams
    Nov 7 at 19:04










  • Ok, but again, "logged out" doesn't mean anything in the context of us looking at this code. Have you verified that the cookie header is getting sent with the request?
    – miken32
    Nov 7 at 19:10










  • how do i check if its sent ?
    – Gracie williams
    Nov 7 at 19:11










  • Hmm, not sure actually, if you don't have access to the server. Did you try formatting the cookie properly per my answer?
    – miken32
    Nov 7 at 19:15
















"doesn't seem to work" doesn't give us a lot to go on. Does the extra header get sent at all with the request? Have you tried getting rid of all the extra whitespace in that line?
– miken32
Nov 7 at 19:03




"doesn't seem to work" doesn't give us a lot to go on. Does the extra header get sent at all with the request? Have you tried getting rid of all the extra whitespace in that line?
– miken32
Nov 7 at 19:03












doesn't seem to work - The page i request is logged out , cookie doesnt seem to work.. whereas if i send curl request with that cookie file , response is logged in
– Gracie williams
Nov 7 at 19:04




doesn't seem to work - The page i request is logged out , cookie doesnt seem to work.. whereas if i send curl request with that cookie file , response is logged in
– Gracie williams
Nov 7 at 19:04












Ok, but again, "logged out" doesn't mean anything in the context of us looking at this code. Have you verified that the cookie header is getting sent with the request?
– miken32
Nov 7 at 19:10




Ok, but again, "logged out" doesn't mean anything in the context of us looking at this code. Have you verified that the cookie header is getting sent with the request?
– miken32
Nov 7 at 19:10












how do i check if its sent ?
– Gracie williams
Nov 7 at 19:11




how do i check if its sent ?
– Gracie williams
Nov 7 at 19:11












Hmm, not sure actually, if you don't have access to the server. Did you try formatting the cookie properly per my answer?
– miken32
Nov 7 at 19:15




Hmm, not sure actually, if you don't have access to the server. Did you try formatting the cookie properly per my answer?
– miken32
Nov 7 at 19:15












1 Answer
1






active

oldest

votes

















up vote
1
down vote













According to RFC 6265:




Serialize the cookie-list into a cookie-string by processing each
cookie in the cookie-list in order:




  1. Output the cookie's name, the %x3D ("=") character, and the cookie's value.


  2. If there is an unprocessed cookie in the cookie-list, output the characters %x3B and %x20 ("; ").





In other words, no spaces around the equal sign, and each cookie should be separated by a semicolon and a space:



$header = [
'Cookie: InterSecure=AyDzUp5AEKz0ErJYeJF2221lIA$$; ASP.NET_SessionId=eqyrgo545czouimlnqc223f0qyi',
];





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53195932%2fuse-cookie-saved-by-curl-in-stream-get-contents%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote













    According to RFC 6265:




    Serialize the cookie-list into a cookie-string by processing each
    cookie in the cookie-list in order:




    1. Output the cookie's name, the %x3D ("=") character, and the cookie's value.


    2. If there is an unprocessed cookie in the cookie-list, output the characters %x3B and %x20 ("; ").





    In other words, no spaces around the equal sign, and each cookie should be separated by a semicolon and a space:



    $header = [
    'Cookie: InterSecure=AyDzUp5AEKz0ErJYeJF2221lIA$$; ASP.NET_SessionId=eqyrgo545czouimlnqc223f0qyi',
    ];





    share|improve this answer



























      up vote
      1
      down vote













      According to RFC 6265:




      Serialize the cookie-list into a cookie-string by processing each
      cookie in the cookie-list in order:




      1. Output the cookie's name, the %x3D ("=") character, and the cookie's value.


      2. If there is an unprocessed cookie in the cookie-list, output the characters %x3B and %x20 ("; ").





      In other words, no spaces around the equal sign, and each cookie should be separated by a semicolon and a space:



      $header = [
      'Cookie: InterSecure=AyDzUp5AEKz0ErJYeJF2221lIA$$; ASP.NET_SessionId=eqyrgo545czouimlnqc223f0qyi',
      ];





      share|improve this answer

























        up vote
        1
        down vote










        up vote
        1
        down vote









        According to RFC 6265:




        Serialize the cookie-list into a cookie-string by processing each
        cookie in the cookie-list in order:




        1. Output the cookie's name, the %x3D ("=") character, and the cookie's value.


        2. If there is an unprocessed cookie in the cookie-list, output the characters %x3B and %x20 ("; ").





        In other words, no spaces around the equal sign, and each cookie should be separated by a semicolon and a space:



        $header = [
        'Cookie: InterSecure=AyDzUp5AEKz0ErJYeJF2221lIA$$; ASP.NET_SessionId=eqyrgo545czouimlnqc223f0qyi',
        ];





        share|improve this answer














        According to RFC 6265:




        Serialize the cookie-list into a cookie-string by processing each
        cookie in the cookie-list in order:




        1. Output the cookie's name, the %x3D ("=") character, and the cookie's value.


        2. If there is an unprocessed cookie in the cookie-list, output the characters %x3B and %x20 ("; ").





        In other words, no spaces around the equal sign, and each cookie should be separated by a semicolon and a space:



        $header = [
        'Cookie: InterSecure=AyDzUp5AEKz0ErJYeJF2221lIA$$; ASP.NET_SessionId=eqyrgo545czouimlnqc223f0qyi',
        ];






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 7 at 19:17

























        answered Nov 7 at 19:07









        miken32

        22.7k84671




        22.7k84671






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53195932%2fuse-cookie-saved-by-curl-in-stream-get-contents%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Academy of Television Arts & Sciences

            L'Équipe

            1995 France bombings