@@ -1306,55 +1306,19 @@ protected function _callApiCurl(
1306
1306
$ multipart = false , $ app_only_auth = false , $ internal = false
1307
1307
)
1308
1308
{
1309
- $ authorization = null ;
1310
- $ url = $ this ->_getEndpoint ($ method );
1311
- $ request_headers = array ();
1312
- if ($ httpmethod === 'GET ' ) {
1313
- if (! $ app_only_auth ) {
1314
- $ authorization = $ this ->_sign ($ httpmethod , $ url , $ params );
1315
- }
1316
- if (json_encode ($ params ) !== '{} '
1317
- && json_encode ($ params ) !== '[] '
1318
- ) {
1319
- $ url .= '? ' . http_build_query ($ params );
1320
- }
1321
- $ ch = curl_init ($ url );
1322
- } else {
1323
- if ($ multipart ) {
1324
- if (! $ app_only_auth ) {
1325
- $ authorization = $ this ->_sign ($ httpmethod , $ url , array ());
1326
- }
1327
- $ params = $ this ->_buildMultipart ($ method , $ params );
1328
- } else {
1329
- if (! $ app_only_auth ) {
1330
- $ authorization = $ this ->_sign ($ httpmethod , $ url , $ params );
1331
- }
1332
- $ params = http_build_query ($ params );
1333
- }
1334
- $ ch = curl_init ($ url );
1335
- if ($ multipart ) {
1336
- $ first_newline = strpos ($ params , "\r\n" );
1337
- $ multipart_boundary = substr ($ params , 2 , $ first_newline - 2 );
1338
- $ request_headers [] = 'Content-Type: multipart/form-data; boundary= '
1339
- . $ multipart_boundary ;
1340
- }
1309
+ list ($ authorization , $ url , $ params , $ request_headers )
1310
+ = $ this ->_callApiPreparations (
1311
+ $ httpmethod , $ method , $ params , $ multipart , $ app_only_auth
1312
+ );
1313
+
1314
+ $ ch = curl_init ($ url );
1315
+ $ request_headers [] = 'Authorization: ' . $ authorization ;
1316
+ $ request_headers [] = 'Expect: ' ;
1317
+
1318
+ if ($ httpmethod !== 'GET ' ) {
1341
1319
curl_setopt ($ ch , CURLOPT_POST , 1 );
1342
1320
curl_setopt ($ ch , CURLOPT_POSTFIELDS , $ params );
1343
1321
}
1344
- if ($ app_only_auth ) {
1345
- if (self ::$ _oauth_consumer_key === null
1346
- && self ::$ _oauth_bearer_token === null
1347
- ) {
1348
- throw new \Exception ('To make an app-only auth API request, consumer key or bearer token must be set. ' );
1349
- }
1350
- // automatically fetch bearer token, if necessary
1351
- if (self ::$ _oauth_bearer_token === null ) {
1352
- $ this ->oauth2_token ();
1353
- }
1354
- $ authorization = 'Bearer ' . self ::$ _oauth_bearer_token ;
1355
- }
1356
- $ request_headers [] = 'Authorization: ' . $ authorization ;
1357
- $ request_headers [] = 'Expect: ' ;
1358
1322
1359
1323
curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , 1 );
1360
1324
curl_setopt ($ ch , CURLOPT_FOLLOWLOCATION , 0 );
@@ -1404,7 +1368,7 @@ protected function _callApiCurl(
1404
1368
* @param array optional $params The parameters to send along
1405
1369
* @param bool optional $multipart Whether to use multipart/form-data
1406
1370
* @param bool optional $app_only_auth Whether to use app-only bearer authentication
1407
- * @param bool optional $internal Whether to use internal call
1371
+ * @param bool optional $internal Whether to use internal call
1408
1372
*
1409
1373
* @return mixed The API reply, encoded in the set return_format
1410
1374
*/
@@ -1414,54 +1378,18 @@ protected function _callApiNoCurl(
1414
1378
$ app_only_auth = false , $ internal = false
1415
1379
)
1416
1380
{
1417
- $ authorization = null ;
1418
- $ url = $ this ->_getEndpoint ($ method );
1419
- $ hostname = parse_url ($ url , PHP_URL_HOST );
1420
- $ request_headers = array ();
1421
- if ($ httpmethod === 'GET ' ) {
1422
- if (! $ app_only_auth ) {
1423
- $ authorization = $ this ->_sign ($ httpmethod , $ url , $ params );
1424
- }
1425
- if (json_encode ($ params ) !== '{} '
1426
- && json_encode ($ params ) !== '[] '
1427
- ) {
1428
- $ url .= '? ' . http_build_query ($ params );
1429
- }
1430
- } else {
1431
- if ($ multipart ) {
1432
- if (! $ app_only_auth ) {
1433
- $ authorization = $ this ->_sign ($ httpmethod , $ url , array ());
1434
- }
1435
- $ params = $ this ->_buildMultipart ($ method , $ params );
1436
- } else {
1437
- if (! $ app_only_auth ) {
1438
- $ authorization = $ this ->_sign ($ httpmethod , $ url , $ params );
1439
- }
1440
- $ params = http_build_query ($ params );
1441
- }
1442
- if ($ multipart ) {
1443
- $ first_newline = strpos ($ params , "\r\n" );
1444
- $ multipart_boundary = substr ($ params , 2 , $ first_newline - 2 );
1445
- $ request_headers [] = 'Content-Type: multipart/form-data; boundary= '
1446
- . $ multipart_boundary ;
1447
- } else {
1448
- $ request_headers [] = 'Content-Type: application/x-www-form-urlencoded ' ;
1449
- }
1450
- }
1451
- if ($ app_only_auth ) {
1452
- if (self ::$ _oauth_consumer_key === null
1453
- && self ::$ _oauth_bearer_token === null
1454
- ) {
1455
- throw new \Exception ('To make an app-only auth API request, consumer key or bearer token must be set. ' );
1456
- }
1457
- // automatically fetch bearer token, if necessary
1458
- if (self ::$ _oauth_bearer_token === null ) {
1459
- $ this ->oauth2_token ();
1460
- }
1461
- $ authorization = 'Bearer ' . self ::$ _oauth_bearer_token ;
1462
- }
1463
- $ request_headers [] = 'Accept: */* ' ;
1381
+ list ($ authorization , $ url , $ params , $ request_headers )
1382
+ = $ this ->_callApiPreparations (
1383
+ $ httpmethod , $ method , $ params , $ multipart , $ app_only_auth
1384
+ );
1385
+
1386
+ $ hostname = parse_url ($ url , PHP_URL_HOST );
1464
1387
$ request_headers [] = 'Authorization: ' . $ authorization ;
1388
+ $ request_headers [] = 'Accept: */* ' ;
1389
+ $ request_headers [] = 'Connection: Close ' ;
1390
+ if ($ httpmethod !== 'GET ' && ! $ multipart ) {
1391
+ $ request_headers [] = 'Content-Type: application/x-www-form-urlencoded ' ;
1392
+ }
1465
1393
1466
1394
$ context = stream_context_create (array (
1467
1395
'http ' => array (
@@ -1473,7 +1401,7 @@ protected function _callApiNoCurl(
1473
1401
'ignore_errors ' => true
1474
1402
),
1475
1403
'ssl ' => array (
1476
- 'verify_peer ' => true ,
1404
+ 'verify_peer ' => false ,
1477
1405
'cafile ' => __DIR__ . '/cacert.pem ' ,
1478
1406
'verify_depth ' => 5 ,
1479
1407
'peer_name ' => $ hostname
@@ -1511,6 +1439,70 @@ protected function _callApiNoCurl(
1511
1439
return $ reply ;
1512
1440
}
1513
1441
1442
+ /**
1443
+ * Do preparations to make the API call
1444
+ *
1445
+ * @param string $httpmethod The HTTP method to use for making the request
1446
+ * @param string $method The API method to call
1447
+ * @param array $params The parameters to send along
1448
+ * @param bool $multipart Whether to use multipart/form-data
1449
+ * @param bool $app_only_auth Whether to use app-only bearer authentication
1450
+ *
1451
+ * @return array (string authorization, string url, array params, array request_headers)
1452
+ */
1453
+ protected function _callApiPreparations (
1454
+ $ httpmethod , $ method , $ params , $ multipart , $ app_only_auth
1455
+ )
1456
+ {
1457
+ $ authorization = null ;
1458
+ $ url = $ this ->_getEndpoint ($ method );
1459
+ $ request_headers = array ();
1460
+ if ($ httpmethod === 'GET ' ) {
1461
+ if (! $ app_only_auth ) {
1462
+ $ authorization = $ this ->_sign ($ httpmethod , $ url , $ params );
1463
+ }
1464
+ if (json_encode ($ params ) !== '{} '
1465
+ && json_encode ($ params ) !== '[] '
1466
+ ) {
1467
+ $ url .= '? ' . http_build_query ($ params );
1468
+ }
1469
+ } else {
1470
+ if ($ multipart ) {
1471
+ if (! $ app_only_auth ) {
1472
+ $ authorization = $ this ->_sign ($ httpmethod , $ url , array ());
1473
+ }
1474
+ $ params = $ this ->_buildMultipart ($ method , $ params );
1475
+ } else {
1476
+ if (! $ app_only_auth ) {
1477
+ $ authorization = $ this ->_sign ($ httpmethod , $ url , $ params );
1478
+ }
1479
+ $ params = http_build_query ($ params );
1480
+ }
1481
+ if ($ multipart ) {
1482
+ $ first_newline = strpos ($ params , "\r\n" );
1483
+ $ multipart_boundary = substr ($ params , 2 , $ first_newline - 2 );
1484
+ $ request_headers [] = 'Content-Type: multipart/form-data; boundary= '
1485
+ . $ multipart_boundary ;
1486
+ }
1487
+ }
1488
+ if ($ app_only_auth ) {
1489
+ if (self ::$ _oauth_consumer_key === null
1490
+ && self ::$ _oauth_bearer_token === null
1491
+ ) {
1492
+ throw new \Exception ('To make an app-only auth API request, consumer key or bearer token must be set. ' );
1493
+ }
1494
+ // automatically fetch bearer token, if necessary
1495
+ if (self ::$ _oauth_bearer_token === null ) {
1496
+ $ this ->oauth2_token ();
1497
+ }
1498
+ $ aut
6EF5
horization = 'Bearer ' . self ::$ _oauth_bearer_token ;
1499
+ }
1500
+
1501
+ return array (
1502
+ $ authorization , $ url , $ params , $ request_headers
1503
+ );
1504
+ }
1505
+
1514
1506
/**
1515
1507
* Parses the API reply to separate headers from the body
1516
1508
*
0 commit comments