PHP curl method of getting the status code for the response

  • 2020-12-13 18:55:50
  • OfStack

PHP curl can simulate 1 http request from the server side, such as fetching web pages, simulating login, etc. Depending on the options, you can get the response header and body in the return result of curl_exec, but does this have the status of the response? To get the status code, you need to execute curl_exec and then get it through curl_getinfo. Such as:
 
$ch = curl_init (); 
curl_setopt($ch, CURLOPT_URL, 'http://www.google.com.hk'); 
curl_setopt($ch, CURLOPT_TIMEOUT, 200); 
curl_setopt($ch, CURLOPT_HEADER, FALSE); 
curl_setopt($ch, CURLOPT_NOBODY, FALSE); 
#curl_setopt( $ch, CURLOPT_POSTFIELDS, "username=".$username."&password=".$password ); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE); 
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET'); 
curl_exec($ch); 
$httpCode = curl_getinfo($ch,CURLINFO_HTTP_CODE); 

curl_getinfo also can get a lot of other information, please refer to: http: / / php net manual/en/function curl - getinfo. php.
 
 The values of the following options will be used as long shadings ( in option Specified in parameters ) :  

CURLOPT_INFILESIZE:  When you upload 1 File to remote site, this option tells PHP The size of the file you uploaded.  
CURLOPT_VERBOSE:  If you want to CURL Report every 1 An unexpected thing, set this option to 1 Nonzero.  
CURLOPT_HEADER:  If you want to put 1 The header is included in the output. Set this option to 1 Nonzero.  
CURLOPT_NOPROGRESS:  If you don't PHP for CURL Transmission display 1 Process bar, set this option to 1 Nonzero. Note: PHP Automatically set this option to a non-zero value. You should change this option only for debugging purposes.  
CURLOPT_NOBODY:  If you don't want to include it in the output body Section, set this option to be 1 Nonzero.  
CURLOPT_FAILONERROR:  If you want to PHP Error occurring (HTTP The code returns greater than or equal 300) , do not display, set this option to 1 People are non-zero. The default behavior is return 1 Normal page, ignore the code.  
CURLOPT_UPLOAD:  If you want to PHP To prepare for upload, set this option to 1 Nonzero.  
CURLOPT_POST:  If you want to PHP To do it 1 A formal HTTP POST , set this option as 1 Nonzero. this POST Is a common  application/x-www-from-urlencoded  Type, most by HTML Form usage.  
CURLOPT_FTPLISTONLY:  Set this option to a non-zero value, PHP Will list FTP List of directory names.  
CURLOPT_FTPAPPEND:  Set this option to be 1 Nonzero, PHP Instead of overwriting it, a remote file will be applied.  
CURLOPT_NETRC:  Set this option to be 1 Nonzero, PHP Will be in you  ~./netrc  Find the user name and password of the remote site you want to connect to.  
CURLOPT_FOLLOWLOCATION:  Set this option to be 1 A nonzero value ( like   " Location:  " ) The server will treat it as HTTP The head of the 1 Part of the sending ( Notice that this is recursive, PHP Will send shape like   " Location:  The head of the" ) .  
CURLOPT_PUT:  Set this option to be 1 A non-zero value to use HTTP upload 1 A file. This file must be set to upload CURLOPT_INFILE and CURLOPT_INFILESIZE options . 
CURLOPT_MUTE:  Set this option to be 1 Nonzero, PHP for CURL The function will be completely silent.  
CURLOPT_TIMEOUT:  Set up the 1 The number of long reshapes as the maximum duration of how many seconds.  
CURLOPT_LOW_SPEED_LIMIT:  Set up the 1 Number of long shadings that control how many bytes are transferred.  
CURLOPT_LOW_SPEED_TIME:  Set up the 1 Number of long shadings, controlling how many seconds to transmit CURLOPT_LOW_SPEED_LIMIT The specified number of bytes.  
CURLOPT_RESUME_FROM:  pass 1 A long shaping parameter containing the byte offset address, ( The start form you want to move to ) .  
CURLOPT_SSLVERSION:  pass 1 containing SSL Version of the long parameters. The default PHP Will be determined by its own efforts, in more security you must manually set.  
CURLOPT_TIMECONDITION:  pass 1 A long parameter. How to handle it CURLOPT_TIMEVALUE Parameters. You can set this parameter to be TIMECOND_IFMODSINCE  or  TIMECOND_ISUNMODSINCE . This is only used to HTTP .  
CURLOPT_TIMEVALUE:  pass 1 A from 1970-1-1 The number of seconds to the present. This time will be CURLOPT_TIMEVALUE Option is used as the specified value, or is default TIMECOND_IFMODSINCE Use.  
 The values of the following options will be treated as strings:  
  

CURLOPT_URL:  This is what you want to use PHP To retrieve the URL Address. You can use it, too curl_init() Set this option when the function is initialized.  
CURLOPT_USERPWD:  pass 1 A form such as [username]:[password] Style string , role PHP To connect to.  
CURLOPT_PROXYUSERPWD:  pass 1 A form such as [username]:[password]  Formatted string to concatenate HTTP The agent.  
CURLOPT_RANGE:  pass 1 A range that you want to specify. It should be" X-Y "Format, X or Y They are excluded. HTTP Teleport also supports several intervals, separated by funny sentences (X-Y,N-M) .  
CURLOPT_POSTFIELDS:  pass 1 as HTTP  " POST A string of all data operated on.  
CURLOPT_REFERER:  in HTTP Included in the request 1 A" referer The string of the ".  
CURLOPT_USERAGENT:  in HTTP Included in the request 1 A" user-agent The string of the ".  
CURLOPT_FTPPORT:  pass 1 Included are ftp  " POST "Used by the command IP Address. this POST The instruction tells the remote server to connect to what we specify IP Address. This string could be 1 a IP The address, 1 Host names, 1 Three web interface names ( in UNIX Under the ) , or ' -'( Use system defaults IP address ) .  
CURLOPT_COOKIE:  pass 1 containing HTTP cookie The head of the.  
CURLOPT_SSLCERT:  pass 1 containing PEM A string of formatted certificates.  
CURLOPT_SSLCERTPASSWD:  pass 1 Individual inclusion use CURLOPT_SSLCERT The password required for the certificate.  
CURLOPT_COOKIEFILE:  pass 1 containing cookie String of the file name of the data. this cookie The file could be Netscape Format, or heap stored in a file HTTP Style head.  
CURLOPT_CUSTOMREQUEST:  When doing HTTP When requested, pass 1 A character is GET or HEAD Use. For the purpose of DELETE Or other operation is beneficial, more Pass a string to be used instead of GET or HEAD when doing an HTTP request. This is useful for doing or another, more obscure, HTTP request.  Pay attention to :  Do not do this until you have confirmed that your server supports commands. The following options are required 1 Description of file ( Through the use of fopen() Function to obtain ) :  
CURLOPT_FILE:  This file will be the output file you put into the transfer, default is STDOUT. 
CURLOPT_INFILE:  This file is the input file you sent over.  
CURLOPT_WRITEHEADER:  This file contains the header portion of your output.  
CURLOPT_STDERR:  This file was written incorrectly instead of stderr . An example to get the page that needs to be logged in , The current practice is to log in perhaps every time 1 time , People who need to make improvements . 

Related articles: