我用 /usr/bin/curl 编写的代码有效,但我用 php curl 编写的代码不起作用。我在哪里犯了错误?
The code I write with /usr/bin/curl works, but the code I write with php curl is not working. Where am I making a mistake?
我在我正在使用的终端上编写了下面的两个代码脚本,我正在使用 php 编写,但它不起作用,并且我收到了缺少参数的警报。
Terminal: /usr/bin/curl -s -X POST "https://api.xxxxxxx.com/action.json?type=modifycontact&resellerno=3040267&resellerpwd=xxxxxxx&lang=tr&responsetype=json" -d "registrycode=81386095&ownercontactid=81045261&admincontactid=81045261&billcontactid=81045261&techcontactid=81045261"
PHP:
$url = "https://api.xxxxxxx.com/action.json?type=modifycontact&resellerno=3040267&resellerpwd=xxxxxxx&lang=tr&responsetype=json";
$value = "registrycode=81386095&ownercontactid=81045261&admincontactid=81045261&billcontactid=81045261&techcontactid=81045261";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $value);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_NOBODY, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);
curl_setopt($ch, CURLOPT_TIMEOUT, 3);
//$son = array();
$sonlu = curl_exec($ch);
$son['sonuc'] = (isset($sonlu) AND !empty($sonlu)) ? $sonlu : json_encode($sunucuhata);
$son['httpcode'] = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$son['error'] = curl_error($ch);
$son['errorno'] = curl_errno($ch);
curl_close($ch);
print_r($son);
Terminal response: {"status":"success","description":"OK"}
PHP response: Array
(
[sonuc] => {"status":"error","description":"4553 missing parameter"}
[httpcode] => 200
[error] =>
[errorno] => 0
)
错误消息不是来自 curl,而是 API 响应。所以 API 需要一些东西。
您可以检查是否有此 API 的文档,其中对错误消息进行了更详细的解释。
我在终端 curl 和 php 之间看到的唯一区别是:php 默认情况下不设置用户代理 header。也许您的请求中缺少此 header。我不知道你的卷曲版本,但这样的东西可以工作:
curl_setopt($ch, CURLOPT_USERAGENT, 'curl/7.61.1');
更新: 如 hanshenrik
所述
curl_setopt($ch, CURLOPT_USERAGENT, 'curl/' . curl_version()['version']);
我在我正在使用的终端上编写了下面的两个代码脚本,我正在使用 php 编写,但它不起作用,并且我收到了缺少参数的警报。
Terminal: /usr/bin/curl -s -X POST "https://api.xxxxxxx.com/action.json?type=modifycontact&resellerno=3040267&resellerpwd=xxxxxxx&lang=tr&responsetype=json" -d "registrycode=81386095&ownercontactid=81045261&admincontactid=81045261&billcontactid=81045261&techcontactid=81045261"
PHP:
$url = "https://api.xxxxxxx.com/action.json?type=modifycontact&resellerno=3040267&resellerpwd=xxxxxxx&lang=tr&responsetype=json";
$value = "registrycode=81386095&ownercontactid=81045261&admincontactid=81045261&billcontactid=81045261&techcontactid=81045261";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $value);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_NOBODY, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);
curl_setopt($ch, CURLOPT_TIMEOUT, 3);
//$son = array();
$sonlu = curl_exec($ch);
$son['sonuc'] = (isset($sonlu) AND !empty($sonlu)) ? $sonlu : json_encode($sunucuhata);
$son['httpcode'] = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$son['error'] = curl_error($ch);
$son['errorno'] = curl_errno($ch);
curl_close($ch);
print_r($son);
Terminal response: {"status":"success","description":"OK"}
PHP response: Array
(
[sonuc] => {"status":"error","description":"4553 missing parameter"}
[httpcode] => 200
[error] =>
[errorno] => 0
)
错误消息不是来自 curl,而是 API 响应。所以 API 需要一些东西。
您可以检查是否有此 API 的文档,其中对错误消息进行了更详细的解释。
我在终端 curl 和 php 之间看到的唯一区别是:php 默认情况下不设置用户代理 header。也许您的请求中缺少此 header。我不知道你的卷曲版本,但这样的东西可以工作:
curl_setopt($ch, CURLOPT_USERAGENT, 'curl/7.61.1');
更新: 如 hanshenrik
所述 curl_setopt($ch, CURLOPT_USERAGENT, 'curl/' . curl_version()['version']);