Implement request Forwarding (curl) and request redirection in PHP

Source: Internet
Author: User
1. Request redirection 1. Use http header information, that is, use the php header function. The HEADER function in PHP is used to send control commands that should have been specified by the HTTP protocol to the browser through the WEB server, such as declaring the type of returned information (Context-type: xxxxxx ), page attributes (Nocache, Expire) and so on. HTTP header information

1. Request redirection 1. Use http header information, that is, use the php header function. The HEADER function in PHP is used to send control commands that should have been specified by the HTTP protocol to the browser through the WEB server, such as declaring the type of the returned information (Context-type: xxx/xxx), page attributes (No cache, Expire), and so on. HTTP header information

1. Request redirection

1,HTTP header information

That is, use the php header function. The HEADER function in PHP is used to send control commands that should have been specified by the HTTP protocol to the browser through the WEB server, such as declaring the type of returned information ("Context-type: xxx/xxx "), page attributes (" No cache "," Expire "), and so on.

Use the HTTP header to redirect PHP to another page as follows:

 
  1. $ Url = "www.baidu.com ";
  2. If (! Empty ($ url ))
  3. {
  4. Header ("HTTP/1.1 303 See Other"); // This statement can be left empty
  5. Header ("Location: $ url ");
  6. }
  7. ?>

Note that "Localtion:" is followed by a space.

2,Mark PHP redirection with HTML

The HTML Tag is the REFRESH tag of META, for example:

 
  1. if (!empty($url))
  2. {
  3. echo " < strong> HTTP-EQUIV=\"Refresh\" CONTENT=\"0;URL=$url\" >"; <>
  4. }
  5. ?>

3,PHP redirection using scripts

Example:

 
  1. if (isset($url))
  2. {
  3. echo " ";?>

Ii. Request forwarding

PHP uses curl for request forwarding

1.Install and enable curl for PHP

1. Install php_curl in Windows

1. Copy the three php_curl.dll files (possibly in the ext folder), libeay32.dll and ssleay32.dll files in the PHP installation folder to % windir %/system32;
2. Open php. ini (which may be in the installation directory of the PHP environment, which is in the c: \ WINDOWS directory by default) and remove the semicolon in the; extension = php_curl.dll header;
3. Restart the php Service (apache or IIS), such as Restarting IIS and running iisreset.

Ii. How to install curl extension in php in linux

If php has been compiled in the system and new extensions need to be added later, one way is to re-compile php completely, and the other way is to compile the extension library separately, extension.

Scenario 1: install php and curl

First download the curl installation package (My php is 4.4.4, and the latest curl 7.16 cannot be used, and the latest curl 7.14 is only available for download. Pay attention to the version)

Then install cURL

# Tar-zxf (enter the downloaded curl package name here) #./configure-prefix =/usr/local/curl # make; make install

When installing php, you only need to turn on the switch-with-curl =/usr/local/curl.

Php requires that the curl Directory should have the include and lib directories, and the include directory should have the easy. h and curl. h files, and the lib directory should have libcurl.. After compilation, the include file is available, but the lib directory may not. It was originally generated in the lib/. libs directory, So copy to the lib directory.

Scenario 2: php has been installed. Now you need to add this extension.

Go to the source code directory for installing the original php file. (If you delete the file but still have the original compressed file, decompress it again)

Cd ext/curlphpize./configure-with-curl = DIR (dir can be left empty if no directory is specified during installation of your curl) make

The curl. so file is generated under PHPDIR/ext/curl/moudles.
Copy the curl. so file to the extensions configuration directory and modify php. ini.

The above two methods are used to install php extension cURL in Linux.

2. cURL-related implementation methods for Get and Post requests

However, we found that the request forwarding implemented with curl is different from the request. getrequestdispatcher request forwarding in JSP.

For example:

A. php implements the curl request, which is actually. the php page requests other pages, and then outputs the response content in. on the php page, after calling curl,. php can do other things.

But JSP is different. After jsp implements request forwarding, for example,. jsp requests are redirected to B. jsp, the current. jsp to execute the forwarded resource B. jsp

The same is that the URLs on a. php and a. jsp do not change.

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.