Home >Backend Development >PHP Tutorial >PHP code implements CORS cross-domain processing of response results of Baidu Wenxinyiyan API interface

PHP code implements CORS cross-domain processing of response results of Baidu Wenxinyiyan API interface

王林
王林Original
2023-08-12 12:09:171672browse

PHP code implements CORS cross-domain processing of response results of Baidu Wenxinyiyan API interface

PHP code implements CORS cross-domain processing of response results of Baidu Wenxin Yiyan API interface

In Web development, cross-Origin Resource Sharing ) is a common question. When we request resources under one domain name from a web page under another domain name, if cross-domain processing is not performed, the browser will block the request by default. In actual development, we often need to call third-party interfaces in the front-end page to obtain data. This article will introduce how to use PHP code to implement CORS cross-domain processing of the response results of Baidu Wenxinyiyan API interface.

The Baidu Wenxin Yiyan API interface is an interface that provides random sentences. We can obtain a random Wenxin Yiyan by sending a GET request to the interface. First, we need to use XMLHttpRequest or Fetch API in the front-end page to send a GET request to the Baidu Wenxin Yiyan API interface. Due to the browser's same-origin policy, we need to perform CORS cross-domain processing on the server side.

The following is a simple PHP code example that demonstrates how to implement CORS cross-domain processing of the response results of Baidu Wenxin Yiyan API interface:

<?php
// 百度文心一言 API 接口地址
$url = 'https://v1.hitokoto.cn/';

// 使用 CURL 发送 GET 请求
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// 设置请求头中的 Origin 字段,用于解决 CORS 跨域问题
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'Origin: https://your-domain.com', // 将 your-domain.com 替换为你实际的域名
]);

$response = curl_exec($ch);
curl_close($ch);

// 设置响应头中的 Access-Control-Allow-Origin 字段,允许跨域访问
header('Access-Control-Allow-Origin: https://your-domain.com');
// 其他 CORS 相关响应头可以根据需要进行设置

// 输出 API 响应结果
echo $response;
?>

In the above code, we first Define the address of Baidu Wenxin Yiyan API interface, and then use CURL to send a GET request to the interface. The Origin field is set in the request header, and its value is the domain name where our front-end page is located. Next, we read the data from the API response and output it. Finally, set the Access-Control-Allow-Origin field in the response header to the domain name of our front-end page to allow cross-domain access.

It should be noted that in the above code, we set the values ​​of the Origin field and the Access-Control-Allow-Origin field to the domain name where our front-end page is located. You need to replace "https://your-domain.com" in the code with your actual domain name.

Through the above code example, we can implement CORS cross-domain processing of the response results of Baidu Wenxinyiyan API interface on the PHP server side. In this way, we can call this interface in the front-end page to obtain the data of random sentences. Everyone can make corresponding modifications and expansions according to their actual needs.

Summary:
This article demonstrates how to implement CORS cross-domain processing of the response results of Baidu Wenxinyiyan API interface through PHP code examples. In actual development, for cross-domain issues, we need to handle them on the server side to ensure that the front-end page can normally obtain data from the third-party interface. I hope this article can help everyone solve the problem of cross-domain requests.

The above is the detailed content of PHP code implements CORS cross-domain processing of response results of Baidu Wenxinyiyan API interface. For more information, please follow other related articles on the PHP Chinese website!

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