PHP API调用适用于本地主机、邮递员,但不适用于实时网站



我在php方面相当初级,有一个API,我想从中读取数据。在我的本地主机上,它运行良好,所有输出都如预期。Postman应用程序也是如此。当我从本地主机而不是本地API访问web API时,它甚至可以工作。但一旦我试图在我的直播网站上得到结果,它就会返回NULL。

注意:这是一个WordPress网站。Localhost是通过Xampp完成的。通过SiteGround在线托管。我以前也有过这种工作,但我不知道发生了什么(也许我稍微更改了代码或其他什么(突然停止了工作。API是一个WordPress许可管理器插件,带有预先配置的API。域名/IP/和客户机密并非此处的原始信息。

$url = "https://example.com/wp-json/lmfwc/v2/licenses?consumer_key=ck_xxxx&consumer_secret=cs_yyy";
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "", 
CURLOPT_FOLLOWLOCATION => True,
CURLOPT_HTTPHEADER =>array (
'Accept: application/json'),
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_SSL_VERIFYPEER => false,
));
$response = curl_exec($curl);
print_r($response) ?> <br><br> <?php ;
$json_response =json_encode($response);
print_r($json_response) ?> <br><br> <?php ;
$info_display = curl_getinfo($curl);
print_r ($info_display); ?> <br><br> <?php 
$array_response = json_decode($response);
print_r($array_response); ?> <br> <br><?php 
$err = curl_error($curl);
var_dump($err); ?> <br> <br><?php 
curl_close($curl);

在我的本地主机上,这段代码产生的响应正是我所需要的:

> {"success":true,"data":{"id":"1","orderId":null,"productId":"2037","licenseKey":"1232edwfsd2","expiresAt":null,"validFor":"14","source":"2","status":"3","timesActivated":"0","timesActivatedMax":"1","createdAt":"2020-02-06
> 20:35:56","createdBy":"1","updatedAt":"2020-02-08
> 12:34:20","updatedBy":"1"}}
> 
> "{"success":true,"data":{"id":"1","orderId":null,"productId":"2037","licenseKey":"1232edwfsd2","expiresAt":null,"validFor":"14","source":"2","status":"3","timesActivated":"0","timesActivatedMax":"1","createdAt":"2020-02-06
> 20:35:56","createdBy":"1","updatedAt":"2020-02-08
> 12:34:20","updatedBy":"1"}}"
> 
> Array ( [url] =>
> https://example.com/wp-json/lmfwc/v2/licenses?consumer_key=ck_xxx&consumer_secret=cs_yyy
> [content_type] => application/json; charset=UTF-8 [http_code] => 200
> [header_size] => 657 [request_size] => 250 [filetime] => -1
> [ssl_verify_result] => 0 [redirect_count] => 0 [total_time] =>
> 2.164129 [namelookup_time] => 0.000694 [connect_time] => 0.256472 [pretransfer_time] => 0.40321 [size_upload] => 0 [size_download] =>
> 205 [speed_download] => 94 [speed_upload] => 0
> [download_content_length] => -1 [upload_content_length] => -1
> [starttransfer_time] => 2.164067 [redirect_time] => 0 [redirect_url]
> => [primary_ip] => *censored by author* [certinfo] => Array ( ) [primary_port] => 443 [local_ip] => *censored by author* [local_port] => *censored by author* [http_version] => 3 [protocol] => 2 [ssl_verifyresult] => 0 [scheme]
> => HTTPS [appconnect_time_us] => 403002 [connect_time_us] => 256472 [namelookup_time_us] => 694 [pretransfer_time_us] => 403210
> [redirect_time_us] => 0 [starttransfer_time_us] => 2164067
> [total_time_us] => 2164129 )
> 
> stdClass Object ( [success] => 1 [data] => stdClass Object ( [id] => 1
> [orderId] => [productId] => 2037 [licenseKey] => 1232edwfsd2
> [expiresAt] => [validFor] => 14 [source] => 2 [status] => 3
> [timesActivated] => 0 [timesActivatedMax] => 1 [createdAt] =>
> 2020-02-06 20:35:56 [createdBy] => 1 [updatedAt] => 2020-02-08
> 12:34:20 [updatedBy] => 1 ) )
> 
> string(0) ""
> 
> status: 3 times activated: 0 times activated max: 1 last update:
> 2020-02-08 12:34:20

我的实时网站上的相同代码崩溃了。所以我需要评论一下//print_r($response) ?> <br><br> <?php ;

那么我就可以得到这样的结果:

> "</meta></head></html>"
> 
> Array ( [url] =>
> https://example.com/wp-json/lmfwc/v2/licenses?consumer_key=ck_xxx&consumer_secret=cs_yyy
> [content_type] => text/html [http_code] => 200 [header_size] => 277
> [request_size] => 254 [filetime] => -1 [ssl_verify_result] => 0
> [redirect_count] => 0 [total_time] => 0.01762 [namelookup_time] =>
> 0.000747 [connect_time] => 0.000873 [pretransfer_time] => 0.016955 [size_upload] => 0 [size_download] => 96 [speed_download] => 5647
> [speed_upload] => 0 [download_content_length] => -1
> [upload_content_length] => -1 [starttransfer_time] => 0.017479
> [redirect_time] => 0 [redirect_url] => [primary_ip] => *censored by author*
> [certinfo] => Array ( ) [primary_port] => 443 [local_ip] => *censored by author*
> [local_port] => *censored by author* [http_version] => 3 [protocol] => 2
> [ssl_verifyresult] => 0 [scheme] => HTTPS [appconnect_time_us] =>
> 16862 [connect_time_us] => 873 [namelookup_time_us] => 747
> [pretransfer_time_us] => 16955 [redirect_time_us] => 0
> [starttransfer_time_us] => 17479 [total_time_us] => 17620 )
> 
> 
> 
> string(0) ""
> 
> status: times activated: times activated max: last update:

从我的开发者控制台谷歌浏览器中的"网络"选项卡,我可以看到这一点。所有请求返回状态200:

Request URL: https://example.com/check-in/
Request Method: GET
Status Code: 200 
Remote Address: *censored by author*
Referrer Policy: no-referrer-when-downgrade
cache-control: no-transform, no-cache, no-store, must-revalidate
content-encoding: gzip
content-type: text/html; charset=UTF-8
date: Fri, 03 Apr 2020 20:01:50 GMT
expires: Wed, 11 Jan 1984 05:00:00 GMT
host-header: 192fc2e7e50945beb8231a492d6a8024
link: <https://example.com/wp-json/>; rel="https://api.w.org/", <https://example.com/?p=2463>; rel=shortlink
server: nginx
status: 200
vary: Accept-Encoding
x-httpd: 1
x-proxy-cache: BYPASS
x-proxy-cache-info: D NC:200000 UP:1
:authority: example.com
:method: GET
:path: /check-in/
:scheme: https
accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
accept-encoding: gzip, deflate, br
accept-language: en-US,en;q=0.9,de-DE;q=0.8,de;q=0.7,nl-NL;q=0.6,nl;q=0.5,fr;q=0.4
cache-control: max-age=0
cookie: *censored by author*
referer: https://example.com/
sec-fetch-dest: document
sec-fetch-mode: navigate
sec-fetch-site: same-origin
sec-fetch-user: ?1
upgrade-insecure-requests: 1
user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36

编辑:将以下代码添加到我的php片段的开头,我通过php剪切插件将其添加到wordpress:

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

我在前端得到了回报:

Notice: Trying to get property 'data' of non-object in /home/customer/www/example.com/public_html/wp-content/plugins/insert-php-code-snippet/shortcode-handler.php(65) : eval()'d code on line 32
Notice: Trying to get property 'status' of non-object in /home/customer/www/example.com/public_html/wp-content/plugins/insert-php-code-snippet/shortcode-handler.php(65) : eval()'d code on line 32
status:
Notice: Trying to get property 'data' of non-object in /home/customer/www/example.com/public_html/wp-content/plugins/insert-php-code-snippet/shortcode-handler.php(65) : eval()'d code on line 33
Notice: Trying to get property 'timesActivated' of non-object in /home/customer/www/example.com/public_html/wp-content/plugins/insert-php-code-snippet/shortcode-handler.php(65) : eval()'d code on line 33
times activated:
Notice: Trying to get property 'data' of non-object in /home/customer/www/example.com/public_html/wp-content/plugins/insert-php-code-snippet/shortcode-handler.php(65) : eval()'d code on line 34
Notice: Trying to get property 'timesActivatedMax' of non-object in /home/customer/www/example.com/public_html/wp-content/plugins/insert-php-code-snippet/shortcode-handler.php(65) : eval()'d code on line 34
times activated max:
Notice: Trying to get property 'data' of non-object in /home/customer/www/example.com/public_html/wp-content/plugins/insert-php-code-snippet/shortcode-handler.php(65) : eval()'d code on line 35
Notice: Trying to get property 'updatedAt' of non-object in /home/customer/www/example.com/public_html/wp-content/plugins/insert-php-code-snippet/shortcode-handler.php(65) : eval()'d code on line 35
last update:

在wordpress API可以输出之前,您似乎正在将某些内容写入输出缓冲区。

<meta><head><html>

我会检查您在localhost上的环境是否与生产匹配。例如插件、主题文件和wordpress核心。您的主题文件可能正在回显标头中的某些内容。

其他建议是:

如果邮递员正在生产,那么你的cUrl设置有问题

这是SSL问题吗?假设您的本地主机上没有SSL,您已经禁用了在cUrl上验证SSL。但是,Siteground我阻止了来自本身不使用SSL的SSL站点的cUrl请求。您还需要将编码集设置为空字符串吗?

它已经解决了,但我必须承认,我不知道是什么最终解决了它。但它与Siteground问题无关。最后,我尝试了一千种方法,删除了本应运行php代码的整个子页面。我创建了一个新页面,试了一遍,结果成功了。很奇怪,但谢谢你的提示和窍门。

您应该将以下行放在脚本的第一行中。所有错误都会显示出来,之后,这行应该在生产环境中删除

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

最新更新