使用CURL_EXEC时“无效的API请求”

时间:2016-06-29 01:16:41

标签: php api curl

我有以下调用API的PHP代码:

$forcetest = 1;

$url = "https://www.website.com/api/pointTotal.php?userId=" . $userId;
if ($forcetest || (isset($_SERVER['ON_TEST_SERVER']) && ($_SERVER['ON_TEST_SERVER']))){
    $url = "https://wwwtest.website.com/api/pointTotal.php?userId=" . $userId;
}

$curl = curl_init();
curl_setopt_array($curl, array(
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_URL => $url
));

curl_setopt($curl, CURLOPT_HTTPHEADER, array(
    'sharedSecret: abc123',
));

//curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); //tested to see if this would work, didn't seem to matter

// Only do this if on dev/test
if ($forcetest || (isset($_SERVER['ON_TEST_SERVER']) && ($_SERVER['ON_TEST_SERVER']))) {
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
}

$response = curl_exec($curl);
print($response);
if(!$response){
    die('Error: "' . curl_error($curl) . '" - Code: ' . curl_errno($curl));
}

我的问题是什么时候去生产(即$forcetest = 0;)它工作得很好,调用prod api并使用prod数据库。如果我切换它,它进入wwwtest子域调用测试API,我得到错误返回 {"success":false,"errors":["Invalid API request"]}

“无效的api请求”不是api的代码中的东西,所以在我看来它的东西curl_exec正在做/错误但我无法弄清楚是什么。如果我直接去api,他们都会在我的浏览器中返回正确的信息,所以我知道它已经起作用了。它可能是一个证书问题或该子域/服务器上的某种xss设置导致它出错?我有点不知所措。

提前致谢。

编辑:CLI curl命令和标题输出:

[josh@mac] $ curl -H 'sharedSecret: abc123' 'https://wwwtest.website.com/api/pointTotal.php?userId=1472&rewardsType=f' -vvv
* About to connect() to wwwtest.website.com port 443
*   Trying xxx.xx.xx.xx... connected
* Connected to wwwtest.website.com (xxx.xx.xx.xx) port 443
* successfully set certificate verify locations:
*   CAfile: /etc/pki/tls/certs/ca-bundle.crt
  CApath: none
* SSLv2, Client hello (1):
SSLv3, TLS handshake, Server hello (2):
SSLv3, TLS handshake, CERT (11):
SSLv3, TLS handshake, Server finished (14):
SSLv3, TLS handshake, Client key exchange (16):
SSLv3, TLS change cipher, Client hello (1):
SSLv3, TLS handshake, Finished (20):
SSLv3, TLS change cipher, Client hello (1):
SSLv3, TLS handshake, Finished (20):
SSL connection using RC4-SHA
* Server certificate:
*    subject: /C=US/ST=New Mexico/L=Albuquerque/O=removed/CN=*.website.com
*    start date: 2013-07-29 00:00:00 GMT
*    expire date: 2016-10-05 12:00:00 GMT
*    subjectAltName: wwwtest.website.com matched
*    issuer: /C=US/O=DigiCert Inc/CN=DigiCert Secure Server CA
* SSL certificate verify ok.
> GET /api/pointTotal.php?userId=1472&rewardsType=f HTTP/1.1
> User-Agent: curl/7.15.5 (x86_64-redhat-linux-gnu) libcurl/7.15.5 OpenSSL/0.9.8b zlib/1.2.3 libidn/0.6.5
> Host: wwwtest.website.com
> Accept: */*
> sharedSecret: abc123
> 
< HTTP/1.1 200 OK
< Date: Wed, 29 Jun 2016 18:27:25 GMT
< Server: Apache/2.2.3 (CentOS) DAV/2 mod_ssl/2.2.3 OpenSSL/0.9.8e-fips-rhel5
< Access-Control-Request-Headers: accept, auth-key
< Access-Control-Allow-Origin: https://customeraccess.website.com
< Access-Control-Allow-Headers: sharedSecret
< Content-Length: 36
< Content-Type: text/html
Connection #0 to host wwwtest.website.com left intact
* Closing connection #0
* SSLv3, TLS alert, Client hello (1):
{"userId":"1472","pointTotal":50600}

还将此添加到进行调用的php代码

curl_getinfo($curl, CURLINFO_HEADER_OUT)它打印出一个404这似乎是问题所在,但是它的网址正在cur im immediatley返回正确的结果所以我不确定是什么导致了404

1 个答案:

答案 0 :(得分:0)

抱歉,刚才看到这个问题仍然存在。问题原来是内部DNS不好。代码和卷曲我们都是正确的,我不得不依靠系统管理员来解决它。