php文件没有在命令行中运行...但是在localhost上运行

时间:2017-05-05 19:36:20

标签: php curl command-line

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

如果我只输入echo "test"输出就好了。

现在我向该脚本添加了一个curl请求,以从API中获取一些数据,然后键入print_r $result

不幸的是,结果永远不会输出到命令行。如果我现在在我的MAMP localhost中运行完全相同的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所说的:

*   Trying 63.141.159.120...
* TCP_NODELAY set
* Connected to store-bgf5e.mybigcommerce.com (63.141.159.120) port   443 (#0)
* ALPN, offering http/1.1
* Cipher selection: ALL:!EXPORT:!EXPORT40:!EXPORT56:!aNULL:!LOW:!RC4:@STRENGTH
* successfully set certificate verify locations:
*   CAfile: /Applications/MAMP/Library/OpenSSL/cert.pem CApath: none
* SSL connection using TLSv1.2 / ECDHE-RSA-AES128-GCM-SHA256
* ALPN, server accepted to use http/1.1
* Server certificate:
*  subject: C=AU; ST=New South Wales; L=Ultimo; O=Bigcommerce Pty Ltd; CN=*.mybigcommerce.com
*  start date: Jun  4 00:00:00 2015 GMT
*  expire date: Sep  1 12:00:00 2018 GMT
*  subjectAltName: host "store-bgf5e.mybigcommerce.com" matched cert's "*.mybigcommerce.com"
*  issuer: C=US; O=DigiCert Inc; OU=www.digicert.com; CN=DigiCert SHA2 High Assurance Server CA
*  SSL certificate verify ok.
> 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
< Date: Fri, 05 May 2017 19:26:27 GMT
< Content-Type: application/json
< Transfer-Encoding: chunked
< Connection: keep-alive
< Set-Cookie: fornax_anonymousId=0ff167a5-e158-4ff8-8962-67b19bd4271b; expires=Mon, 03-May-2027 19:26:27 GMT; path=/; domain=.store-bgf5e.mybigcommerce.com
< Last-Modified: Thu, 13 Apr 2017 22:34:54 +0000
< X-BC-ApiLimit-Remaining: 2147483622
< X-BC-Store-Version: 7.6.0
< 
* Curl_http_done: called premature == 0
* Connection #0 to host store-bgf5e.mybigcommerce.com left intact

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

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

2 个答案:

答案 0 :(得分:0)

你可以尝试var_dump你的结果,看看发生了什么。对于你的curl响应,我没有看到任何内容长度标题,我认为curl返回空字符串。

请你发布你的PHP代码吗?

答案 1 :(得分:0)

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

http_parse_headers类的connection方法中,第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命令。

感谢所有想要理解这一点的人:)