在命令行中无法运行php文件...但在本地主机中可以运行

我创建了一个非常简单的小php脚本,希望在我的MacOSX终端上运行它。

如果我只是输入“echo \”测试\””,那就可以输出了。

现在我在脚本中添加了一个curl请求来抓取一些来自API的数据,然后键入“print_r $result”。

不幸的是,结果从未输出到命令行中。如果现在在我的MAMP本地主机中运行完全相同的php文件,则一切正常工作,并且内容正确输出。

这是我的代码:

require 'connection.php';

$store = new connection('danny', 'https://store-bgf5e.mybigcommerce.com/api/v2/', 'XXXXX');
$customers = $store->get('/customers');

foreach($customers AS $customer) {
    print "------ Getting Adresses for: " . $customer['first_name'] . ' '         . $customer['last_name'] . ' -------';
    if( isset($customer['addresses']['resource']) ) {
        print_r($store->get($customer['addresses']['resource']));
    }
}

exit;

我知道API可以很好地返回地址,因此调用肯定有效。

这是Curl在说什么:

*正在尝试63.141.159.120......
* TCP_NODELAY设置
*已连接store-bgf5e.mybigcommerce.com的端口443(#0)
* ALPN,提供http / 1.1
*密码选择:ALL:!EXPORT:!EXPORT40:!EXPORT56:!aNULL:!LOW:!RC4:@STRENGTH
*成功设置了证书验证位置:
* CAfile:/Applications/MAMP/Library/OpenSSL/cert.pem CApath:none
*使用TLSv1.2 / ECDHE-RSA-AES128-GCM-SHA256进行SSL连接
* ALPN,服务器接受使用http / 1.1
*服务器证书:
*主题:C = AU; ST =新南威尔士州; L = Ultimo; O = Bigcommerce Pty Ltd; CN = * .mybigcommerce.com
*开始日期:2015年6月4日00:00:00格林尼治标准时间
*过期日期:2018年9月1日12:00:00 GMT
* subjectAltName:主机“store-bgf5e.mybigcommerce.com”和证书的“* .mybigcommerce.com”匹配
*发行人:C = US; O = DigiCert Inc; OU = www.digicert.com; CN = DigiCert SHA2 High Assurance Server CA
* SSL证书验证通过。
> GET /api/v2/customers HTTP/1.1
Host: store-bgf5e.mybigcommerce.com
Authorization: Basic ZGFubnk6M2QyNjE5NTdjZGNjMTNlYmU1MTJiNDRiMjE1NGJiZGZmMGRjNTUxMw==
Accept: application/json
Content-Type: application/json

< HTTP / 1.1 200 OK
< Server:openresty
<日期:周五,2017年5月5日19:26:27 GMT
< Content-Type:application / json
<传输编码:chunked
<连接:保持活动状态
< Set-Cookie:fornax_anonymousId = 0ff167a5-e158-4ff8-8962-67b19bd4271b; 过期= Mon,03-May-2027 19:26:27 GMT;路径= /;页面= .store-bgf5e.mybigcommerce.com
<最后修改:2017年4月13日22:34:54 +0000
< X-BC-ApiLimit-Remaining:2147483622
< X-BC-Store-Version:7.6.0
<
* Curl_http_done:调用过早== 0
*连接#0到store-bgf5e.mybigcommerce.com已保持不变

对我来说,Curl似乎正在正确执行一切。这几乎就像成功的curl请求阻止任何其他PHP代码运行一样。由于“print \ _r”,我希望看到地址被打印到命令行中。

提前表示感谢您提供的任何提示。

点赞
用户7557441
用户7557441

你可以尝试使用var_dump来查看你的结果,并看看发生了什么。对于你的curl响应,我没有看到任何Content-Length头,我想curl返回了一个空字符串。

请问你能否将你的PHP代码发给我?

2017-05-05 20:15:55
用户3307277
用户3307277

我解决了问题。我使用此框架连接到BigCommerce API:https://github.com/adambilsing/PHP-cURL-lib-for-Bigcommerce-API/blob/master/connection.php

connection类的http_parse_headers方法中,第54行有一个if语句:

if ($retVal['X-Bc-Apilimit-Remaining'] <= 100) {
    sleep(300);
}

即使$retVal['X-Bc-Apilimit-Remaining']null,这也会触发脚本进入睡眠状态。

将其更改为这个修复了问题:

if ( isset($retVal['X-Bc-Apilimit-Remaining']) && $retVal['X-Bc-Apilimit-Remaining'] <= 100) {
    sleep(300);
}

一旦您深入了解,这就很明显了。我甚至不知道PHP中有一个sleep命令。

谢谢所有试图弄清楚这个问题的人 :)

2017-05-10 17:48:38