


PHP CURL simulates JQuery's Ajax request header to add parameters that can be displayed in _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>
<span><span>{</span></span>
<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>
<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>
<span><span>}</span></span>
<span>总结: 设置的头如: A-B-C, 在_SERVER变量中打印出来的key会变成 HTTP_A_B_C的形式.</span>
<span></span>
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.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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

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 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

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

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

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 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

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:
