@@ -375,10 +375,10 @@ public static function wrapResult($result = '', $queryObject = '')
375375 *
376376 * @return Result
377377 * */
378- public static function contentstackRequest ($ queryObject = '' , $ type = '' )
378+ public static function send_request ($ queryObject = '' , $ type = '' )
379379 {
380380 $ server_output = '' ;
381-
381+
382382 if ($ queryObject ) {
383383 if (Utility::isLivePreview ($ queryObject )) {
384384 $ queryObject ->_query ['live_preview ' ] = ($ queryObject ->contentType ->stack ->live_preview ['live_preview ' ] ?? 'init ' );
@@ -399,30 +399,81 @@ public static function contentstackRequest($queryObject = '', $type = '')
399399 if ($ Headers ["branch " ] !== '' && $ Headers ["branch " ] !== "undefined " ) {
400400 $ request_headers [] = 'branch: ' .$ Headers ["branch " ];
401401 }
402+
403+ $ proxy_details = $ queryObject ->contentType ->stack ->proxy ;
404+ $ timeout = $ queryObject ->contentType ->stack ->timeout ;
405+
402406 curl_setopt ($ http , CURLOPT_HTTPHEADER , $ request_headers );
403407
404408 curl_setopt ($ http , CURLOPT_HEADER , false );
405409 // setting the GET request
406410 curl_setopt ($ http , CURLOPT_CUSTOMREQUEST , "GET " );
407411 // receive server response ...
408412 curl_setopt ($ http , CURLOPT_RETURNTRANSFER , true );
409- $ response = curl_exec ($ http );
413+ // set the cURL time out
414+ curl_setopt ($ http , CURLOPT_TIMEOUT_MS , $ timeout );
415+
416+ if (array_***_exists ("url " ,$ proxy_details ) && array_***_exists ("port " ,$ proxy_details )){
417+ if ($ proxy_details ['url ' ] != '' && $ proxy_details ['port ' ] != '' ) {
418+
419+ // Set the proxy IP
420+ curl_setopt ($ http , CURLOPT_PROXY , $ proxy_details ['url ' ]);
421+ // Set the port
422+ curl_setopt ($ http , CURLOPT_PROXYPORT , $ proxy_details ['port ' ]);
423+
424+ }
425+ }
426+ if (array_***_exists ("username " ,$ proxy_details ) && array_***_exists ("password " ,$ proxy_details )){
427+ if ($ proxy_details ['username ' ] != '' && $ proxy_details ['password ' ] != '' ) {
428+
429+ $ proxyauth = $ proxy_details ['username ' ].": " .$ proxy_details ['password ' ];
430+ // Set the username and password
431+ curl_setopt ($ http , CURLOPT_PROXYUSERPWD , $ proxyauth );
432+
433+ }
434+ }
410435
436+ $ response = curl_exec ($ http );
411437 // status code extraction
412438 $ httpcode = curl_getinfo ($ http , CURLINFO_HTTP_CODE );
413-
439+
414440 // close the curl
415441 curl_close ($ http );
416442 if ($ httpcode > 199 && $ httpcode < 300 ) {
417443 // wrapper the server result
418- $ response = Utility::wrapResult ($ response , $ queryObject );
419- } else {
420- throw new CSException ($ response , $ httpcode );
444+ $ response = Utility::wrapResult ($ response , $ queryObject );
445+
446+ }elseif ($ httpcode == 0 ){
447+ $ response = array ( 'error_message ' => 'cURL couldn’t hook up with the proxy IP address and port used ' , 'error_code ' => '407 ' );
448+ }
449+ else {
450+ $ response = Utility::wrapResult ($ response , $ httpcode );
451+ }
452+ }
453+ $ response += ['httpcode ' => $ httpcode ];
454+ return $ response ;
455+ }
456+
457+ public static function contentstackRequest ($ queryObject = '' , $ type = '' , $ count = 0 )
458+ {
459+ $ retryDelay = $ queryObject ->contentType ->stack ->retryDelay ;
460+ $ retryLimit = $ queryObject ->contentType ->stack ->retryLimit ;
461+ $ errorRetry = $ queryObject ->contentType ->stack ->errorRetry ;
462+ $ response = Utility::send_request ($ queryObject , $ type );
463+
464+ if (in_array ( $ response ['httpcode ' ] ,$ errorRetry )){
465+ if ($ count < $ retryLimit ){
466+ $ retryDelay = round ($ retryDelay /1000 ); //converting retry_delay from milliseconds into seconds
467+ sleep ($ retryDelay ); //sleep method requires time in seconds
468+ $ count += 1 ;
469+ $ response = Utility::contentstackRequest ($ queryObject , $ type , $ count );
421470 }
422471 }
472+ unset($ response ['httpcode ' ]);
423473 return $ response ;
424474 }
425475
476+
426477 /**
427478 * Validate the *** is set or not
428479 *
0 commit comments