在尝试访问langSearch API时,cURL请求返回400错误

前端开发 2026-07-09

已解决。内置了所有错误检查的工作代码如下:

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);
if(curl_exec($curl) === false){
    echo 'Curl error: ' . curl_error($ch);
}

$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;
}

我在使用PHP的 cURL调用langSearch API时遇到了400错误。

从他们的文档来看,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
}'

将cURL转换成PHP - 我写了这个来访问API。

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

这似乎已经连通,但详细日志显示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

当我 var_dump($response) 时,唯一的回应是“null”。

解决方案

首先,你对数据进行了编码,但把未编码的数据以数组形式发送:

$payload = json_encode($params);
curl_setopt($curl, CURLOPT_POSTFIELDS, $params); // use $payload here

第二个是头信息:

$headers = [
    "Authorisation: Bearer " . $LANGSEARCH_KEY,
    "Content Type: application/json"
];

单词 Authorisation 拼写错误 - 应该用Z 而不是s - Authorization,并检查 Content Type 是否写得正确。

站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。

相关文章