PHP cURL调用langSearch API报400错误的排查及解决代码
PHP cURL调用langSearch API报400错误且返回null的解决方法
问题描述
调用langSearch的v1/web-search接口时,出现400 Bad Request错误,var_dump($response)返回null。官方提供的bash cURL示例如下:
curl --location 'https://api.langsearch.com/v1/web-search' \ --header 'Authorization: Bearer YOUR-API-KEY' \ --header 'Content-Type: application/json' \ --data '{ "query": "tell me the highlights from Apple 2024 ESG report", "freshness": "noLimit", "summary": true, "count": 10 }'
我编写的PHP代码如下:
function web_search_with_langsearch($query) { $LANGSEARCH_KEY = "sk-nnnnnnnnnnnnnnnnnnnnn"; $ENDPOINT = "https://api.langsearch.com/v1/web-search"; $params = [ "query" => $query, "freshness" => "noLimit", "summary" => true, "count" => 10 ]; $headers = [ "Authorisation: Bearer " . $LANGSEARCH_KEY, "Content Type: application/json" ]; $payload = json_encode($params); $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $ENDPOINT); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_VERBOSE, true); curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); curl_setopt($curl, CURLOPT_POSTFIELDS, $params); $streamVerboseHandle = fopen('php://temp', 'w+'); curl_setopt($curl, CURLOPT_STDERR, $streamVerboseHandle); $response = curl_exec($curl); curl_close($curl); rewind($streamVerboseHandle); $verboseLog = stream_get_contents($streamVerboseHandle); echo "cUrl verbose information:\n", "<pre>", htmlspecialchars($verboseLog), "</pre>\n"; $jsonContent = json_decode(strip_tags($response), true); var_dump($jsonContent); return $jsonContent; }
verbose日志显示400错误:
PHP Messages: cUrl verbose information: * About to connect() to api.langsearch.com port 443 (#0) * Trying 47.236.117.232... * Connected to api.langsearch.com (47.236.117.232) port 443 (#0) * Initializing NSS with certpath: sql:/etc/pki/nssdb * CAfile: /etc/pki/tls/certs/ca-bundle.crt CApath: none * SSL connection using TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 * Server certificate: * subject: CN=api.langsearch.com * start date: Aug 04 00:00:00 2025 GMT * expire date: Aug 03 23:59:59 2026 GMT * common name: api.langsearch.com * issuer: CN=Encryption Everywhere DV TLS CA - G2,OU=www.digicert.com,O=DigiCert Inc,C=US > POST /v1/web-search HTTP/1.1 Host: api.langsearch.com Accept: */* Authorisation: Bearer sk-nnnnnnnnnnnnnnnnnnn Content Type: application/json Content-Length: 447 Expect: 100-continue Content-Type: multipart/form-data; boundary=----------------------------3770c64b7bb1 < HTTP/1.1 100 Continue < HTTP/1.1 400 < Date: Sat, 30 May 2026 17:04:42 GMT < Content-Type: text/html;charset=utf-8 < Content-Length: 435 < Connection: keep-alive < Content-Language: en * HTTP error before end of send, stop sending < * Closing connection 0
错误原因分析
从日志和代码中可以定位几个关键问题:
- 请求头拼写错误:
Authorisation应为Authorization,Content Type应为Content-Type(字段名中间是连字符,不是空格) - POST数据格式错误:传递了数组
$params而非JSON字符串$payload,导致cURL自动将请求内容类型设为multipart/form-data,不符合API要求的application/json - 缺少错误检查:未捕获cURL执行过程中的错误,无法及时排查问题
修正后的可运行代码
function web_search_with_langsearch($query) { $LANGSEARCH_KEY = "sk-nnnnnnnnnnnnnnnnnnnnnn"; $ENDPOINT = "https://api.langsearch.com/v1/web-search"; $params = [ "query" => $query, "freshness" => "noLimit", "summary" => true, "count" => 2 ]; $headers = [ "Authorization: Bearer " . $LANGSEARCH_KEY, "Content-Type: application/json" ]; $payload = json_encode($params); $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $ENDPOINT); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_VERBOSE, true); curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); curl_setopt($curl, CURLOPT_POSTFIELDS, $payload); $streamVerboseHandle = fopen('php://temp', 'w+'); curl_setopt($curl, CURLOPT_STDERR, $streamVerboseHandle); // 添加cURL执行错误检查 if(curl_exec($curl) === false){ echo 'Curl error: ' . curl_error($curl); } $response = curl_exec($curl); curl_close($curl); rewind($streamVerboseHandle); $verboseLog = stream_get_contents($streamVerboseHandle); echo "cUrl verbose information:\n", "<pre>", htmlspecialchars($verboseLog), "</pre>\n"; var_dump($response); $jsonContent = json_decode(strip_tags($response), true); var_dump($jsonContent); return $jsonContent; }
关键修正点
- 修复了请求头的拼写和格式错误,确保符合API的要求
- 将POST数据改为传递JSON编码后的字符串,保证请求体为JSON格式
- 添加了cURL执行错误检查,便于快速定位问题
- 调整
count参数为2(可选修改,不影响核心功能)
内容的提问来源于stack exchange,提问作者TJS101
相关产品推荐
相关产品推荐

