php - file_get_contents hangs, but I set timeout paramer

349

I found a strange problem. this is my php problem code.

static function sendImgMsg($openid, $mediaid, $access)                                                                                                                                                   
{                                                                                                                                                                                                        
    $csSelf= <<<EOF                                                                                                                                                                                      
{                                                                                                                                                                                                            
"touser":"$openid",                                                                                                                                                                                      
"msgtype":"image",                                                                                                                                                                                       
"image":                                                                                                                                                                                                 
{                                                                                                                                                                                                        
  "media_id":"$mediaid"                                                                                                                                                                                  
}                                                                                                                                                                                                                                                                                                                                                                                                                 
EOF;                                                                                                                                                                                                         
    $ret = Util::postRaw('https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=' . $access, $csSelf);                                                                                      
    return json_decode($ret, true);                                                                                                                                                                      
}

static function postRaw($url, $data)
{   
    $opts = array (
        'http' => array (
            'method' => 'POST',
            'header'=> "Content-type: application/x-www-form-urlencoded\r\n" . 
            "Content-Length: " . strlen($data) . "\r\n",
            'content' => $data,
            'timeout' => 60
            )   
        );  

    $context = stream_context_create($opts);
    return file_get_contents($url, false, $context);
}   

I call sendImgMsg function to send a post data to specific server. Sometimes call it will hang. Finally I found it hangs in file_get_contents api. I will show my strace picture about file_get_contents. enter image description here

It hangs in read system call. but fd is nonblock. Why? Any suggestions are appreciated.

People are also looking for solutions to the problem: Php SimpleTest CodeCoverage

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.