Php Twitter Web 服务 curl 和 file_get_contents 将字符串响应转换为 JSON
Php Twitter Web services curl and file_get_contents converting string response to JSON
我正在尝试将 twitter API 与 PHP CURL 一起使用(之前我使用 file_get_contents
,由于 file_get_content
无法有效响应,我不得不放弃推特速率限制)。
当我 运行 使用 file_get_content
的代码时,我得到一个响应字符串,我可以在其上轻松使用 json_decode()
并完成我的工作。但是...
当我 运行 使用 curl 的代码时,在响应中我得到一个巨大的字符串,它具有与我使用上述方法获得的相同数据以及一些附加信息。因此,我不能在该回复中使用 json_decode()
。以下是两个回复。
来自file_get_content
的回复
string(1964) "{"id":2988119635,"id_str":"2988119635","name":"Michael Jackson","screen_name":"Yahoo6464","location":"","description":"","url":null,"entities":{"description":{"urls":[]}},"protected":false,"followers_count":4,"friends_count":2,"listed_count":2,"created_at":"Sun Jan 18 07:18:41 +0000 2015","favourites_count":6,"utc_offset":null,"time_zone":null,"geo_enabled":false,"verified":false,"statuses_count":144,"lang":"en","status":{"created_at":"Tue Mar 15 07:09:21 +0000 2016","id":709637592510308352,"id_str":"709637592510308352","text":"noooooooooooooooooooo"}"
来自 CURL 的响应
string(2942) "HTTP/1.1 200 OK
cache-control: no-cache, no-store, must-revalidate, pre-check=0, post-check=0
content-disposition: attachment; filename=json.json
content-length: 1964
content-type: application/json;charset=utf-8
date: Wed, 16 Mar 2016 06:58:18 GMT
expires: Tue, 31 Mar 1981 05:00:00 GMT
last-modified: Wed, 16 Mar 2016 06:58:18 GMT
pragma: no-cache
server: tsa_f
set-cookie: lang=en; Path=/
set-cookie: guest_id=v1%3A145811149839783410; Domain=.twitter.com; Path=/; Expires=Fri, 16-Mar-2018 06:58:18 UTC
status: 200 OK
strict-transport-security: max-age=631138519
x-access-level: read-write
x-connection-hash: e23e2992def7a3837cdbb3a3201bf7de
x-content-type-options: nosniff
x-frame-options: SAMEORIGIN
x-rate-limit-limit: 15
x-rate-limit-remaining: 12
x-rate-limit-reset: 1458112290
x-response-time: 171
x-transaction: 3fca373925e5f65b
x-twitter-response-tags: BouncerExempt
x-twitter-response-tags: BouncerCompliant
x-xss-protection: 1; mode=block
{"id":2988119635,"id_str":"2988119635","name":"Michael Jackson","screen_name":"Yahoo6464","location":"","description":"","url":null,"entities":{"description":{"urls":[]}},"protected":false,"followers_count":4,"friends_count":2,"listed_count":2,"created_at":"Sun Jan 18 07:18:41 +0000 2015","favourites_count":6,"utc_offset":null,"time_zone":null,"geo_enabled":false,"verified":false,"statuses_count":144,"lang":"en","status":{"created_at":"Tue Mar 15 07:09:21 +0000 2016","id":709637592510308352,"id_str":"709637592510308352","text":"noooooooooooooooooooo"}"
这是我的卷曲选项
$options = array(
CURLOPT_URL => $url,
CURLOPT_HEADER => true,
CURLINFO_HEADER_OUT => true,
CURLOPT_HTTPHEADER => $header,
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_CUSTOMREQUEST => 'GET',
);
$ci = curl_init();
$d = curl_setopt_array($ci, $options);
$response = curl_exec($ci);
只需将 CURLOPT_HEADER
设置为 false
。
我正在尝试将 twitter API 与 PHP CURL 一起使用(之前我使用 file_get_contents
,由于 file_get_content
无法有效响应,我不得不放弃推特速率限制)。
当我 运行 使用 file_get_content
的代码时,我得到一个响应字符串,我可以在其上轻松使用 json_decode()
并完成我的工作。但是...
当我 运行 使用 curl 的代码时,在响应中我得到一个巨大的字符串,它具有与我使用上述方法获得的相同数据以及一些附加信息。因此,我不能在该回复中使用 json_decode()
。以下是两个回复。
来自file_get_content
的回复string(1964) "{"id":2988119635,"id_str":"2988119635","name":"Michael Jackson","screen_name":"Yahoo6464","location":"","description":"","url":null,"entities":{"description":{"urls":[]}},"protected":false,"followers_count":4,"friends_count":2,"listed_count":2,"created_at":"Sun Jan 18 07:18:41 +0000 2015","favourites_count":6,"utc_offset":null,"time_zone":null,"geo_enabled":false,"verified":false,"statuses_count":144,"lang":"en","status":{"created_at":"Tue Mar 15 07:09:21 +0000 2016","id":709637592510308352,"id_str":"709637592510308352","text":"noooooooooooooooooooo"}"
来自 CURL 的响应
string(2942) "HTTP/1.1 200 OK
cache-control: no-cache, no-store, must-revalidate, pre-check=0, post-check=0
content-disposition: attachment; filename=json.json
content-length: 1964
content-type: application/json;charset=utf-8
date: Wed, 16 Mar 2016 06:58:18 GMT
expires: Tue, 31 Mar 1981 05:00:00 GMT
last-modified: Wed, 16 Mar 2016 06:58:18 GMT
pragma: no-cache
server: tsa_f
set-cookie: lang=en; Path=/
set-cookie: guest_id=v1%3A145811149839783410; Domain=.twitter.com; Path=/; Expires=Fri, 16-Mar-2018 06:58:18 UTC
status: 200 OK
strict-transport-security: max-age=631138519
x-access-level: read-write
x-connection-hash: e23e2992def7a3837cdbb3a3201bf7de
x-content-type-options: nosniff
x-frame-options: SAMEORIGIN
x-rate-limit-limit: 15
x-rate-limit-remaining: 12
x-rate-limit-reset: 1458112290
x-response-time: 171
x-transaction: 3fca373925e5f65b
x-twitter-response-tags: BouncerExempt
x-twitter-response-tags: BouncerCompliant
x-xss-protection: 1; mode=block
{"id":2988119635,"id_str":"2988119635","name":"Michael Jackson","screen_name":"Yahoo6464","location":"","description":"","url":null,"entities":{"description":{"urls":[]}},"protected":false,"followers_count":4,"friends_count":2,"listed_count":2,"created_at":"Sun Jan 18 07:18:41 +0000 2015","favourites_count":6,"utc_offset":null,"time_zone":null,"geo_enabled":false,"verified":false,"statuses_count":144,"lang":"en","status":{"created_at":"Tue Mar 15 07:09:21 +0000 2016","id":709637592510308352,"id_str":"709637592510308352","text":"noooooooooooooooooooo"}"
这是我的卷曲选项
$options = array(
CURLOPT_URL => $url,
CURLOPT_HEADER => true,
CURLINFO_HEADER_OUT => true,
CURLOPT_HTTPHEADER => $header,
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_CUSTOMREQUEST => 'GET',
);
$ci = curl_init();
$d = curl_setopt_array($ci, $options);
$response = curl_exec($ci);
只需将 CURLOPT_HEADER
设置为 false
。