PDA

View Full Version : سوال: curl - خطای 504 Gateway Timeout



crafcrab
سه شنبه 25 مهر 1396, 09:32 صبح
سلام دوستان
من در php با curl به یک api وصل میشم و یکسری پارامتر براش POST میکنم در قالب xml و یکسری داده از اون میگیرم در جواب به صورت xml تا اینجا مشکلی نیست
مشکل زمانی هست که سروری که api روش هست بعضی وقت ها خطای 504 میده (حتی با POST MAN)
و من نمیتونم این خطا رو مدیریت کنم
در اصل کد های من بعد از اجرای دستور :

$output=curl_exec($ch);
دیگه کار نمیکنه و مستقیم در صفحه مرورگر برای کاربر مینویسه 504 Gateway Timeout
چطوری باید این خطا رو handling کنم و پیام خطای خودم روبه کاربر نمایش بدم!!!
تنضیمات php ini من:


max_input_time=60max_execution_time=60upload_max_f ilesize=1000000Mpost_max_size=259200Mmemory_limit= 1024M
اینم کد من:




error_reporting(E_ALL); set('display_errors','off');ini_set("log_errors",1);set_time_limit(0);ignore_user_abort(true);
class myclass{ $api_url='http://abc.ir:8080/mypage'; public $content_type='application/xml'; public $accept='application/xml'; public $authorization='dgdfgdfgdgvdfgdgf'; public $curl_timeout=60; public $curl_debug_file='log.txt'; function get_data($xml_content) { try{ if(filter_var($this->api_url,FILTER_VALIDATE_URL)){ $ch=curl_init($this->api_url); curl_setopt($ch,CURLOPT_HTTPHEADER,array('Content-Type: '.$this->content_type,'Accept: '.$this->accept,'Authorization: '.$this->authorization)); curl_setopt($ch,CURLOPT_POST,1); curl_setopt($ch,CURLOPT_POSTFIELDS,"$xml_content"); curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); curl_setopt($ch,CURLOPT_TIMEOUT,$this->curl_timeout); curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,0); curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,0); // error reporting curl_setopt($ch,CURLOPT_VERBOSE,true); $verbose=fopen($this->curl_debug_file,'a'); curl_setopt($ch,CURLOPT_STDERR,$verbose);
// execute request $output=curl_exec($ch);
// debug result $response=curl_getinfo($ch,CURLINFO_HTTP_CODE); //check if 504 return
// close connections curl_close($ch);
if($response=='200'){ // get response from api $output=new SimpleXMLElement($output); return array(true,$output); } else{ return array(false,'1003'); } } else{ return array(false,'1002'); } }catch(Exception $e){ return array(false,'1001'); } }}


وقتی api به من 504 بده بعد از اجرای خطی که کد :


$output=curl_exec($ch);


داخلش هست و کد های بعد آن اجرا نمیشود در مرورگر کاربر در یک صفحه سفید مینویسه Gateway Timeout

نکته : اگر سروری که api میده درست باشه من کدم کار میکنه بعضی مواقع فقط api به من 504 میده

plague
سه شنبه 25 مهر 1396, 11:48 صبح
برای کنترل اررور از try/catch میتونی استفاده کنی

crafcrab
سه شنبه 25 مهر 1396, 13:52 عصر
دوست عزیر اگر کد من رو تو یک فایل php کپی میکردی میدیدی که این دستورات رو گذاشتم

حتی برای curl هم کنترل خطا انجام دادم

حتی برای جوابی که از curl میاد responsive هدر هم چک میکنم که 200 باشه

crafcrab
جمعه 28 مهر 1396, 13:56 عصر
کسی بلد نیست!!!!

Unique
سه شنبه 02 آبان 1396, 10:18 صبح
نمیدونم مشکلت حل شده یا نه. ولی نگفتی $response توی این حالت چی برمیگردونه ؟! احتمالا نباید ۲۰۰ باشه ! مشکلت مربوط به سرور میشه و اونها باید تنظیماتشون را بهینه کنن.