我使用/ usr / bin / curl编写的代码有效,但是使用php curl编写的代码不起作用。我在哪里出错?

时间:2018-12-29 21:08:59

标签: php curl php-curl

我在正在使用的终端上编写了以下两个代码脚本,使用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
)

1 个答案:

答案 0 :(得分:1)

错误消息不是来自curl,而是API响应。因此,API需要一些东西。

您可以检查是否有该API的文档,该文档详细说明了错误消息。

我看到的终端curl和php之间的唯一区别是:php默认情况下不设置USER AGENT标头。您的请求中可能缺少此标头。我不知道您的curl版本,但是类似的方法可能有效:

 curl_setopt($ch, CURLOPT_USERAGENT, 'curl/7.61.1');

更新:如hanshenrik

所述
 curl_setopt($ch, CURLOPT_USERAGENT, 'curl/' . curl_version()['version']);