PHP Curl function Exec_ch and get_headers

Source: Internet
Author: User

Yesterday, in the project, someone is judging whether a video under YouTube can be played, write down the following code:
       $request _url= ' http://youtube.com/get_video_info?video_id= '.$vid. ' &el=vevo&fmt=18&asv=2&hd=1 '; $ch=Curl_init (); curl_setopt ($ch, Curlopt_url,$request _url); curl_setopt ($ch, Curlopt_returntransfer, 1); curl_setopt ($ch, Curlopt_connecttimeout, 10); curl_setopt ($ch, Curlopt_followlocation,True); $data= Curl_exec ($ch); Curl_close ($ch); unset($ch); if($data===false) { $this->deletevideo ($video[' ID ']); Echo"Program:".$vid. "Program ID".$video[' ID ']. "Invalid, delete. \ n"; Continue; } Parse_str($data,$details); unset($data); if(!isset($details[' Url_encoded_fmt_stream_map ']) ||Empty($details[' Url_encoded_fmt_stream_map '])) { $this->deletevideo ($video[' ID ']); Echo"Program:".$vid. "Program ID".$video[' ID ']. "Invalid, delete. \ n"; Continue; } $newstr=Explode(",",$details[' Url_encoded_fmt_stream_map ']); $str 1=Explode("&",$newstr[0]); $obj=Array(); for($j= 0;$j<Count($str 1);$j++) { $str 2=Explode("=",$str 1[$j]); if(!isset($obj[$str 2[0]])) { $obj[$str 2[0]] =$str 2[1]; } } $ary _re[' source_url '] =UrlDecode($obj[' URL ']); if(Empty($ary _re[' Source_url ']) || !isset($ary _re[' Source_url '])) { $this->deletevideo ($video[' ID ']); Echo"Program:".$vid. "Program ID".$video[' ID ']. "Invalid, delete. \ n"; Continue; } Else { $bool=$this->getmobilecurl ($ary _re[' Source_url ']);//not able to play if($bool===false) { $this->deletevideo ($video[' ID ']); Echo"Program:".$vid. "Program ID".$video[' ID ']. "Invalid, delete. \ n"; Continue; } } $right _nums++;
The functions of Getmobilecurl are defined as follows:
$ch=Curl_init (); curl_setopt ($ch, Curlopt_url,$request _url); curl_setopt ($ch, Curlopt_returntransfer, 1); curl_setopt ($ch, Curlopt_connecttimeout, 10); curl_setopt ($ch, Curlopt_followlocation,True); $data= Curl_exec ($ch); Curl_close ($ch); unset($ch); if($data===false) {                return false             }

Use Curl_exec to determine if the current connection can play, and there is a problem:

Prompt for memory overflow!

I specifically debug a bit, found that the $data data accounted for a large amount of memory, open the $request_url, that is, YouTube video of the actual playing address, only to find that the video is HD 1080P, and is 1 hours of length, this only to find the problem is the cause of the $ Data this is the content of the video, so it overflows,

Decisively the Getmobilecurl function is processed to obtain the server's response header information:

functionGetmobilecurl ($url)    {        $res=get_headers($url, 1); $response _status=$res[0]; if(Strpos($response _status, "200")!==false||Strpos($response _status, "302")!==false||Strpos($response _status, "301")!==false) {            return true; } Else {            return false; }    }

Problem solving:

When using the Curl function for crawler processing, pay attention to whether the target is a video, files and other relatively large goals, as well as their own needs.

PHP Curl function Exec_ch and get_headers

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.