PHP curl fetch as json 不工作

PHP curl fetch as json not working

我有以下 PHP 脚本,假设它转到 URL 并从该页面获取 JSON 输出:

function nexusCheck($nexusUrl, $toolName, $verboseBool, $httpCode, $connectTime, $totalTime) {
    global $date;
    $username = 'user';
    $password = 'password';
    $URL = "$nexusUrl" . "/service/siesta/capabilities";
    $handle = curl_init();
    $timeout = 30;
    curl_setopt($handle, CURLOPT_URL, $URL);
    curl_setopt($handle, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($handle, CURLOPT_CONNECTTIMEOUT, $timeout);
    curl_setopt($handle, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
    //curl_setopt($handle, CURLOPT_HEADER, array('Accept: application/json' , 'Content-Type: application/json'));
    curl_setopt($handle, CURLOPT_HEADER, "Accept: application/json");
    curl_setopt($handle, CURLOPT_USERPWD, "{$username}:{$password}");
    //var_dump(curl_setopt($handle, CURLOPT_USERPWD, "{$username}:{$password}"));
    $result = curl_exec($handle);

    //echo encode_json($result);
    //echo decode_json($result);
    echo($result);

    curl_close($handle);
}

如前所述,输出应该是 JSON,但是,现在输出仅设置为文本。我不确定为什么会发生这种情况,因为当我实际在终端上进行实际卷曲时,输出结果为 JSON!

我的cURL语句是:curl -X GET -H "Accept: application/json" -u username:password "http://nexus.company.net/service/siesta/capabilities".

知道为什么输出只是文本吗?我在这里做错了什么?

编辑:澄清一下,我的意思是页面上显示的结果未编码为 JSON 数据结构。但是,当我在终端中执行 curl 语句时,输出已正确编码。

EDIT2:好的,输出如下所示:

000d43932f6eb3f3truesmartproxy.event.publishrepositoryio-aws-internal-releasesfeedEnabledfalseIO AWS Company Internal Hosted ReleasestruefalseSmart Proxy: PublishActiveCategorySmart ProxyRepositoryIO AWS Company Internal Hosted Releases00028650f5fea946truesmartproxy.event.publishrepositoryio-thirdpartyfeedEnabledfalseIO 3rd partytruefalseSmart Proxy: PublishActiveCategorySmart ProxyRepositoryIO 3rd party000d43932f6eb3f1truesmartproxy.event.publishrepositoryio-aws-internal-snapshotsfeedEnabledfalseIO AWS Company Internal Hosted SnapshotstruefalseSmart Proxy: PublishActiveCategorySmart ProxyRepositoryIO AWS Company Internal Hosted Snapshots000d43932f6eb3f2falsesmartproxy.event.subscriberepositoryio-aws-internal-snapshotspreemptiveFetchfalsedeletetruefeedEnabledfalseIO AWS Company Internal Hosted SnapshotsfalsefalseSmart Proxy: SubscribeDisabledCategorySmart ProxyRepositoryIO AWS Company Internal Hosted Snapshots000d43932f6eb3f0truesmartproxy.event.publishrepositorypublicfeedEnabledfalsePublic RepositoriestruefalseSmart Proxy: PublishActiveCategorySmart ProxyRepositoryPublic Repositories00028650f5fea949truesmartproxy.event.subscriberepositoryio-core-releasespreemptiveFetchtruedeletetruefeedEnabledfalseIO Core Services ReleasestruefalseSmart Proxy: SubscribeActive<h2>Monitor State</h2> <br/> <div> <pre>CONNECTED</pre> </div> <br/> <h2>Remote Node ID</h2> <br/> <div> <pre>8FA5D941-31598C8F-B7C4203F-EDD413CA-B94B6C03</pre> </div> <br/> <h2>Remote Connection URI</h2> <br/> <div> <pre>ssl://nexus.company.net:51773</pre> </div> <br/> <h2>Remote Repository ID</h2> <br/> <div> <pre>core-release-proxy</pre> </div> <br/> <h2>Handshake Status</h2> <br/> <div> <pre>ENABLED</pre> </div> <br/> <h2>Remote Certificate Fingerprint</h2> <br/> <div> <pre>8F:A5:D9:41:31:59:8C:8F:B7:C4:20:3F:ED:D4:13:CA:B9:4B:6C:03</pre> </div> <br/> <h2>Remote Certificate PEM</h2> <br/> <div> <pre>-----BEGIN CERTIFICATE----- MIIDijCCAnKgAwIBAgIGAVQ0GItxMA0GCSqGSIb3DQEBBQUAMIGEMS0wKwYDVQQD DCRmNWQ3ODQ1MS1jNmJkLTQzOTQtYTU2OS1iODY5OWE4ZDk1MmUxDjAMBgNVBAsM BLAHBLAHBLAHBLAHBLAHBLHKLHJKDLJFKDLJKFLDJKFLDJKLFDJKFDJKLFDJKLFJ usgJWixHE5e3JZD7ZaBlhmKcBf/gF6HWNOrhh6exV/PTUnQreLaMSQ6gt/2zP/3e mCHR0iMOXPDCI8nKCGxPuNGoTR/aY5ZBihJeCPTSRoV6+6G0ATCqwM6QxePJk/yT 64f8rHRdD+6sJKr3lzBVXTIYGz0bVK+AKCRA+MaeQT0bcYU6UJMGF5jKPKUPEg== -----END CERTIFICATE----- </pre> </div> <br/> <h2>Remote Certificate Detail</h2> <br/> <div> <pre> [0] Version: 3 SerialNumber: 1461162904433 IssuerDN: CN=f5d78451-c6bd-4394-a569-b8699a8d952e,OU=Nexus,O=Sonatype,L=Silver Spring,ST=MD,C=US Start Date: Wed Apr 20 14:35:04 UTC 2016 Final Date: Fri Mar 27 14:35:04 UTC 2116 SubjectDN: CN=f5d78451-c6bd-4394-a569-b8699a8d952e,OU=Nexus,O=Sonatype,L=Silver Spring,ST=MD,C=US Public Key: RSA Public Key modulus: 98a2f8f258a8348f5b3bcee954cdf59dccb052c9bf17e1a514cdff759accc44e7f42e2773blahblahblahblajblahlbhalbhlahblhalbhalhblahblahblahblahlbhalbhalhblahblac99f586c2c2035b37889aff642120cdd147d54d3e09d8ad5c213b8bfae3133633660df75e1 public exponent: 10001 Signature Algorithm: SHA1WITHRSA Signature: aac0ce90c5e3c993fc93eb87fcac745d0feeac24 aaf79730555d32181b3d1b54af80282440f8c69e 413d1b71853a5093061798ca3ca50f12 </pre> </div> <br/> <h2>Shared Connection Detail</h2> <br/> <div> <pre>ActiveMQConnection {id=ID:ip-10-96-2-8-44490-1465326994959-58:1,clientId=ID:ip-10-96-2-8-44490-1465326994959-57:1,started=true}</pre> </div> <br/> CategorySmart ProxyRepositoryIO Core Services Releases00028650f5fea948truesmartproxy.event.publishrepositoryio-core-releasesfeedEnabledfalseIO Core Services ReleasestruefalseSmart Proxy: PublishActiveCategorySmart ProxyRepositoryIO Core Services Releases00028650f5fea947truesmartproxy.event.subscriberepositoryio-thirdpartypreemptiveFetchtruedeletetruefeedEnabledfalseIO 3rd partytruefalseSmart Proxy: SubscribeActive<h2>Monitor State</h2> <br/> <div> <pre>CONNECTED</pre> </div> <br/> <h2>Remote Node ID</h2> <br/> <div> <pre>8FA5D941-B94B6C03</pre> </div> <br/> <h2>Remote Connection URI</h2> <br/> <div> <pre>ssl://nexus.company.net:51773</pre> </div> <br/> ..................................................................................................................................................

而不是(我从命令行得到的):

[ {
  "capability" : {
    "id" : "000d43932f6eb3f3",
    "notes" : null,
    "enabled" : true,
    "typeId" : "smartproxy.event.publish",
    "properties" : [ {
      "key" : "repository",
      "value" : "io-aws-internal-releases"
    }, {
      "key" : "feedEnabled",
      "value" : "false"
    } ]
  },
  "description" : "IO AWS Company Internal Hosted Releases",
  "active" : true,
  "error" : false,
  "typeName" : "Smart Proxy: Publish",
  "stateDescription" : "Active",
  "status" : null,
  "tags" : [ {
    "key" : "Category",
    "value" : "Smart Proxy"
  }, {
    "key" : "Repository",
    "value" : "IO AWS Company Internal Hosted Releases"
  } ]
}, {
  "capability" : {
    "id" : "00028650f5fea946",
    "notes" : null,
    "enabled" : true,
    "typeId" : "smartproxy.event.publish",
    "properties" : [ {
      "key" : "repository",
      "value" : "io-thirdparty"
    }, {
      "key" : "feedEnabled",
      "value" : "false"
    } ]
  },
  "description" : "IO 3rd party",
  "active" : true,
  "error" : false,
  "typeName" : "Smart Proxy: Publish",
  "stateDescription" : "Active",
  "status" : null,
  "tags" : [ {
    "key" : "Category",
    "value" : "Smart Proxy"
  }, {
    "key" : "Repository",
    "value" : "IO 3rd party"
  } ].........................................................................................................................

EDIT3:所以 说得更好,我想发生的事情是当我尝试在 PHP 中发出与命令行 cURL 相同的请求时,当您从 PHP.

发出请求时,服务器 returns 的格式不同

EDIT4:所以使用 HTTPHEADER 而不是 HEADER 并将 header 作为数组发送就可以了,但是有一个小问题,如果键的值具有 HTML 标记浏览器使用这些标记格式化页面。所以像这样:

它应该是这样的:

  "stateDescription" : "Active",
  "status" : "<h2>Node ID</h2>\n<br/>\n<div>\n    <pre>8FA5D941-31598C8F-B7C4203F-EDD413CA-B94B6C03</pre>\n</div>\n\n<br/>\n\n<h2>Fingerprint</h2>\n<br/>\n<div>\n    <pre>8F:A5:D9:41B9:4B:6C:03</pre>\n</div>\n\n<br/>\n\n<h2>Certificate</h2>\n<br/>\n<div>\n    <pre>-----BEGIN CERTIFICATE-----\nMIIDijCCAnKgAwIBAgIGAVQ0GItxMA0GCSqGSIb3DQEBBQUAMIGEMS0wKwYDVQQD\nDCRmNWQ3ODQ1MS1jNmJkLTQzOTQtYTU2OS1iODY5OWE4ZDk1MmUxDjAMBgNVBAsM\nBU5leHVzMREw6exV/PTUnQreLaMSQ6gt/2zP/3e\nmCHR0iMOXPDCI8nKCGxPuNGoTR/aY5ZBihJeCPTSRoV6+6G0ATCqwM6QxePJk/yT\n64f8rHRdD+6sJKr3lzBVXTIYGz0bVK+AKCRA+MaeQT0bcYU6UJMGF5jKPKUPEg==\n-----END CERTIFICATE-----\n</pre>\n        </div>\n\n<br/>\n\n<h2>Detail</h2>\n<br/>\n<div>\n    <pre>[\n[\n  Version: V3\n  Subject: C=US, ST=MD, L=Silver Spring, O=Sonatype, OU=Nexus, CN=f5d78451-c6bd-4394-a569-b8699a8d952e\n  Signature Algorithm: SHA1withRSA, OID = 1.2.840.113549.1.1.5\n\n  Key:  Sun RSA public key, 2048 bits\n  modulus: 19268587064139719085609236135424479219819536352682784106558145114346679106038989267437271230004063018121065830291975586740542592294259900243345540709378803877292510984799053572694764430270406900807137\n  public exponent: 65537\n  Validity: [From: Wed Apr 20 14:35:04 UTC 2016,\n               To: Fri Mar 27 14:35:04 UTC 2116]\n  Issuer: C=US, ST=MD, L=Silver Spring, O=Sonatype, OU=Nexus, CN=f5d78451-c6bd-4394-a569-b8699a8d952e\n  SerialNumber: [    01543418 8b71]\n\n]\n  Algorithm: [SHA1withRSA]\n  Signature:\n0000: 72 16 A7 0E 0A 83 74 97   BC 9E FD FD F6 53 34 33  r.....t......S43\n0010: AF 0D 9B 67 84 73 2E A2   93 79 81 7F 41 B8 91 C9   C0 CE 90 C5 E3 C9 93  F.z....0........\n00D0: FC 93 EB 87 FC AC 74 5D   0F EE AC 24 AA F7 97 30  ......t]...$...0\n00E0: 55 5D 32 18 1B 3D 1B 54   AF 80 28 24 40 F8 C6 9E  U]2..=.T..($@...\n00F0: 41 3D 1B 71 85 3A 50 93   06 17 98 CA 3C A5 0F 12  A=.q.:P.....<...\n\n]</pre>\n</div>\n",
  "tags" : [ {
    "key" : "Category",
    "value" : "Smart Proxy"
  } ]

相反,我得到了这个:

问题 1:您没有发送 Accept: application/json header 因为您没有使用 CURLOPT_HTTPHEADER,您正在使用 CURLOPT_HEADER,并且您没有使用一个数组,而是一个字符串。修复这两个问题。

问题 2:您没有指定任何 CURLOPT_USERAGENT,阻止不包含 user-agent header 的请求变得越来越流行。您也应该解决这个问题,只是为了确定。

和提示:将 CURLOPT_ENCODING 设为空字符串,这样您的传输将神奇地变得更快。