Home Backend Development PHP Tutorial PHP CURL simulates JQuery's Ajax request header to add parameters that can be displayed in _SERVER

PHP CURL simulates JQuery's Ajax request header to add parameters that can be displayed in _SERVER

Aug 08, 2016 am 09:20 AM
ajax curl server

<span></span>Scenario: The project has two different ways of handling ajax requests and ordinary requests, such as error handling. When an error occurs when accessing through the browser, jump to the DEBUG page. As shown below (this is a framework written by myself, ThinkPHP of style copy, because I am just a pure technical person. Sorry - -):


OK Let’s get back to the topic. If it is an Ajax request, I need to return the information in JSON format. Convenient for front-end (JS, IOS, ANDROID, etc.). Such as:


Then, on the server side, we need a mark to help us identify whether this request is an ajax request or a normal request. What I refer to here is the JQuery method, and many people As we all know, js can be set through the setRequestHeader method of the XMLHttpRequest object. But what if the request is issued by the server through CURL. The processing method is as follows:

<span>function </span><span>Get</span><span>(</span><span>$url</span><span>)</span>
Copy after login
<span><span>{</span></span>
Copy after login
<span><span><span></span>$ch </span><span>= </span><span><em>curl_init</em></span><span>();
</span><span><em><span></span>curl_setopt</em></span><span>(</span><span>$ch</span><span>, </span><span><em>CURLOPT_URL</em></span><span>, </span><span>$url</span><span>);
</span><span><em><span></span>curl_setopt</em></span><span>(</span><span>$ch</span><span>, </span><span><em>CURLOPT_HEADER</em></span><span>, </span><span>0</span><span>);
</span><span><em><span></span>curl_setopt</em></span><span>(</span><span>$ch</span><span>, </span><span><em>CURLOPT_RETURNTRANSFER</em></span><span>, </span><span>1</span><span>);</span></span>
Copy after login
<span></span><pre class="brush:php;toolbar:false"><span><span><span></span>$HEADER </span><span>= </span><span>array</span><span>(
</span><span><span></span></span><span>'X-REQUESTED-WITH: XMLHTTPREQUEST'
</span><span><span></span>);
</span><span><em><span></span>curl_setopt</em></span><span>(</span><span>$ch</span><span>, </span><span><em>CURLOPT_HTTPHEADER</em></span><span>, </span><span>$HEADER</span><span>);
</span><span><span></span>$return </span><span>= </span><span><em>curl_exec</em></span><span>(</span><span>$ch</span><span>);
</span><span><em><span></span>curl_close</em></span><span>(</span><span>$ch</span><span>);
</span><span></span><span><span></span>return </span><span>$return</span><span>;</span></span>
Copy after login
<span><span>}</span></span>
Copy after login
In this way, you can use $_SERVER['HTTP_X_REQUESTED_WITH in PHP ']; Go get it.
<span>总结: 设置的头如: A-B-C, 在_SERVER变量中打印出来的key会变成 HTTP_A_B_C的形式.</span>
Copy after login
<span></span>
Copy after login
Copy after login

Copyright statement: This article is an original article by the blogger and may not be reproduced without the permission of the blogger.

The above introduces how PHP CURL simulates JQuery's Ajax request header and adds parameters that can be displayed in _SERVER, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Tutorial on updating curl version under Linux! Tutorial on updating curl version under Linux! Mar 07, 2024 am 08:30 AM

To update the curl version under Linux, you can follow the steps below: Check the current curl version: First, you need to determine the curl version installed in the current system. Open a terminal and execute the following command: curl --version This command will display the current curl version information. Confirm available curl version: Before updating curl, you need to confirm the latest version available. You can visit curl's official website (curl.haxx.se) or related software sources to find the latest version of curl. Download the curl source code: Using curl or a browser, download the source code file for the curl version of your choice (usually .tar.gz or .tar.bz2

How to solve the 403 error encountered by jQuery AJAX request How to solve the 403 error encountered by jQuery AJAX request Feb 20, 2024 am 10:07 AM

Title: Methods and code examples to resolve 403 errors in jQuery AJAX requests. The 403 error refers to a request that the server prohibits access to a resource. This error usually occurs because the request lacks permissions or is rejected by the server. When making jQueryAJAX requests, you sometimes encounter this situation. This article will introduce how to solve this problem and provide code examples. Solution: Check permissions: First ensure that the requested URL address is correct and verify that you have sufficient permissions to access the resource.

How to handle 301 redirection of web pages in PHP Curl? How to handle 301 redirection of web pages in PHP Curl? Mar 08, 2024 am 11:36 AM

How to handle 301 redirection of web pages in PHPCurl? When using PHPCurl to send network requests, you will often encounter a 301 status code returned by the web page, indicating that the page has been permanently redirected. In order to handle this situation correctly, we need to add some specific options and processing logic to the Curl request. The following will introduce in detail how to handle 301 redirection of web pages in PHPCurl, and provide specific code examples. 301 redirect processing principle 301 redirect means that the server returns a 30

How to install, uninstall, and reset Windows server backup How to install, uninstall, and reset Windows server backup Mar 06, 2024 am 10:37 AM

WindowsServerBackup is a function that comes with the WindowsServer operating system, designed to help users protect important data and system configurations, and provide complete backup and recovery solutions for small, medium and enterprise-level enterprises. Only users running Server2022 and higher can use this feature. In this article, we will explain how to install, uninstall or reset WindowsServerBackup. How to Reset Windows Server Backup If you are experiencing problems with your server backup, the backup is taking too long, or you are unable to access stored files, then you may consider resetting your Windows Server backup settings. To reset Windows

How to solve jQuery AJAX request 403 error How to solve jQuery AJAX request 403 error Feb 19, 2024 pm 05:55 PM

jQuery is a popular JavaScript library used to simplify client-side development. AJAX is a technology that sends asynchronous requests and interacts with the server without reloading the entire web page. However, when using jQuery to make AJAX requests, you sometimes encounter 403 errors. 403 errors are usually server-denied access errors, possibly due to security policy or permission issues. In this article, we will discuss how to resolve jQueryAJAX request encountering 403 error

PHP and Ajax: Building an autocomplete suggestion engine PHP and Ajax: Building an autocomplete suggestion engine Jun 02, 2024 pm 08:39 PM

Build an autocomplete suggestion engine using PHP and Ajax: Server-side script: handles Ajax requests and returns suggestions (autocomplete.php). Client script: Send Ajax request and display suggestions (autocomplete.js). Practical case: Include script in HTML page and specify search-input element identifier.

How to solve the problem of jQuery AJAX error 403? How to solve the problem of jQuery AJAX error 403? Feb 23, 2024 pm 04:27 PM

How to solve the problem of jQueryAJAX error 403? When developing web applications, jQuery is often used to send asynchronous requests. However, sometimes you may encounter error code 403 when using jQueryAJAX, indicating that access is forbidden by the server. This is usually caused by server-side security settings, but there are ways to work around it. This article will introduce how to solve the problem of jQueryAJAX error 403 and provide specific code examples. 1. to make

How to get variables from PHP method using Ajax? How to get variables from PHP method using Ajax? Mar 09, 2024 pm 05:36 PM

Using Ajax to obtain variables from PHP methods is a common scenario in web development. Through Ajax, the page can be dynamically obtained without refreshing the data. In this article, we will introduce how to use Ajax to get variables from PHP methods, and provide specific code examples. First, we need to write a PHP file to handle the Ajax request and return the required variables. Here is sample code for a simple PHP file getData.php:

See all articles