PHP Post data with Fsockopen

前端 未结 10 1525
梦如初夏
梦如初夏 2020-12-01 15:20

I am attempting to post data using fsockopen, and then returning the result. Here is my current code:



        
相关标签:
10条回答
  • 2020-12-01 15:51

    Is using cURL and option?

    0 讨论(0)
  • 2020-12-01 15:53

    At no point is $data being written to the socket. You want to add something like:

    $out .= "Connection: Close\r\n\r\n";
    fwrite($fp, $out);
    fwrite($fp, $data);
    
    0 讨论(0)
  • 2020-12-01 15:53

    you can use this technique it will help to call as many as pages you like all pages will run at once independently without waiting for each page response as asynchronous.

    cornjobpage.php //mainpage

        <?php
    
    post_async("http://localhost/projectname/testpage.php", "Keywordname=testValue");
    //post_async("http://localhost/projectname/testpage.php", "Keywordname=testValue2");
    //post_async("http://localhost/projectname/otherpage.php", "Keywordname=anyValue");
    //call as many as pages you like all pages will run at once independently without waiting for each page response as asynchronous.
                ?>
                <?php
    
                /*
                 * Executes a PHP page asynchronously so the current page does not have to wait for it to     finish running.
                 *  
                 */
                function post_async($url,$params)
                {
    
                    $post_string = $params;
    
                    $parts=parse_url($url);
    
                    $fp = fsockopen($parts['host'],
                        isset($parts['port'])?$parts['port']:80,
                        $errno, $errstr, 30);
    
                    $out = "POST ".$parts['path']."?$post_string"." HTTP/1.1\r\n";//you can use GET instead of POST if you like
                    $out.= "Host: ".$parts['host']."\r\n";
                    $out.= "Content-Type: application/x-www-form-urlencoded\r\n";
                    $out.= "Content-Length: ".strlen($post_string)."\r\n";
                    $out.= "Connection: Close\r\n\r\n";
                    fwrite($fp, $out);
                    fclose($fp);
                }
                ?>
    

    testpage.php

        <?
        echo $_REQUEST["Keywordname"];//case1 Output > testValue
        ?>
    

    PS:if you want to send url parameters as loop then follow this answer :https://stackoverflow.com/a/41225209/6295712

    0 讨论(0)
  • 2020-12-01 15:54

    Try this instead

    $out .= 'Content-Length: ' . strlen($data) . '\r\n';
    $out .= "Connection: Close\r\n\r\n";
    $out .= $data;
    
    0 讨论(0)
提交回复
热议问题