Содержание
- PHP curl put 500 error
- 5 Answers 5
- PHP script returning 500 error — Using cURL to send to API
- 2 Answers 2
- php cUrl POST with SSL certificate end with an error 500
- 2 Answers 2
- Related
- Hot Network Questions
- Subscribe to RSS
- curl_exec dies with error 500, while uploading to s3
- 1 Answer 1
PHP curl put 500 error
I have a curl put request that works fine on my localhost but on the live server it throws back a 500 error. Here is my code:
I do have curl enabled on the server!
Does anyone have any ideas why it is not working on the server? I am on shared hosting if that helps.
I also have enabled error reporting at the top of the file but no errors show after the curl has completed. I just get the generic 500 error page.
I have been in contact with the client and they have confirmed that the information that is sent is received by them and inserted into their back office system. So it must be something to do with the response that is the cause. It is a small block of xml that is suppose to be returned.
I have tried the same script on a different server and heroku and I still get the same result.
I think I may have found the route of the issue. The script seems to be timing out because of a timeout on FastCGI and because I am on shared hosting I can not change it. Can any one confirm this?
I got in contact with my hosting provider and they confirmed that the script was timing out due to the timeout value on the server not the one I can access with any PHP function or ini_set().
5 Answers 5
If the error is, like you think it is, to do with a script timeout and you do not have access to the php.ini file — there is an easy fix
simply use set_time_limit(INT) where INT is the number of seconds, at the beginning of your script to override the settings in the php.ini file
Setting a timeout of set_time_limit(128) should solve all your problems and is generally accepted as a reasonable upper limit
Here are a few things to try:
Remove the variability in the script — for testing, hardcode the session id, so that the curl curl is the same. You cannot reliably test something, if it changes each time you run it.
Try using curl directly from the command line, via something like curl http://*****************/cgi-bin/commctrl.pl?SessionId=12345&SystemId=live . This will show you if the problem is really due to the computer itself, or something to do with PHP.
Check the logs on your server, probably something like /var/log/apache/error.log depending on what OS your server uses. Also look at the access log, so that you can see whether you are actually receiving the same request.
Finally, if you really run out of ideas, you can use a program like wireshark or tcpdump/WinDump to monitor the connection, so that you can compare the packets being sent from each computer. This will give you an idea of how they are different — are they being mangled by a firewall? Is php adding extra headers to one of them? Are different CURL defaults causing different data to be included?
Источник
PHP script returning 500 error — Using cURL to send to API
I am working with an API to update our product information. The API allows me to do a bulk item upload of 20 items per call. In order to update all 1000+ items I have to loop through this script a number of times. This script basically compiles an XML of 20 items and uses curl to send it, then grabs the next 20 items creates XML and repeat. When I limit the outter loop to, say 15, it works fine and returns a success message for each API call. It fails at random times I’ve had it send up to 40 API calls before failing and I’ve also had it fail after 10 calls.
I can’t find anything in the error logs as to why I am getting a 500 back. I’ve reached out to the folks managing the API to see if they have any idea.
My best guess is it has something to do with a timeout.
2 Answers 2
Looks like you have a bug (maybe just a typo in your question?)
$xml should be $xml_output there no? Also, unless you’re using it somewhere else later on, why write to ./bulk-item-pusher.xml ?
Add to your error handling a bit to check for error responses from the remote API
Since you say the timeout doesn’t appear to be the problem, I’ll second @Jay Blanchard’s comment above; it’s probably the data you’re sending on some of the requests.
At that point I would take each of the 20 items that was sent and send them individually to determine if one of the items you’re sending is causing the remote side to crash.
From there determine what the problem is with that particular data item. I’d also follow up with some communication with the folks maintaining that API once you figure it out. 500’s usually mean «our server couldn’t cope with the request». It would be nice if they could revise it to handle such a case and send you back a 200 response with a component of the payload indicating any items that couldn’t be processed, indexed by some type of correlation identifier.
Источник
php cUrl POST with SSL certificate end with an error 500
I’ve got a PHP cUrl session that works well with HTTP URLs but ends with an error 500 with https.
I’ve already tried to use
but it didn’t work.
Here you have my code:
And here a print_r of $curl_info:
Thank you very much for your help!!
2 Answers 2
A 500 error is a generic error on the remote server, so probably nothing much that you can debug. You may need to contact the devs of the remote server to find out why their HTTPS site is giving a 500 error. – aynber
That was finally the right answer. The problem wasn’t in my code but on server side.
Thank you for pointing me on the right direction!
I read on OpenSSL site that some versions are not yet supported. There is also no back compatibility or no more compatibility between php-5.6 or some CURL versions. I had recompiled a different version of openssl, that is their LTS program. All work flawless
Hot Network Questions
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.1.14.43159
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.
Источник
curl_exec dies with error 500, while uploading to s3
I am uploading a large file with curl (php) to amazon s3, using POST request.
When the file is small (several MBs) — it works. When the file is couple of hundred MB — the script dies with error 500 at the curl_exec line (I know it exactly, because I have placed logging right before and after it).
The curl initialization looks like
I assume, that the problem is out of memory (or something alike). I have requested my hosting provider to show me the error_log, but maybe there is something I could do now.
Thank you a lot!
1 Answer 1
If your own server is die()ing with 500 error, you may be running out of memory, try these steps.
Since you can’t view your errorlog, ensure errors are output from your script file:
Increase the memory limit in your php.ini, or preferably, do not load the entire file into memory before sending it (give curl a file pointer, not the entire file content).
An example can be seen in this excellent PHP S3 library:
If it’s Amazon returning a 500 error, this would be the method to determine why:
From here: Response status codes beginning with the digit «5» indicate cases in which the server is aware that it has encountered an error or is otherwise incapable of performing the request. Except when responding to a HEAD request, the server should include an entity containing an explanation of the error situation, and indicate whether it is a temporary or permanent condition.
Output the response header and the response body to look for a description of the reason:
The cause should be mentioned in there somewhere.
Источник
Hi there,
I’m writing an application that pulls from two separate web based APIs, collates the data into a single array of data. Once done, it then enters a loop that cycles through the array and executes a cURL call for each cycle. This worked perfectly for a controlled set of 10 artificial records and so I’ve started testing with live data.
On running the code, I eventually the following :
Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
I’m not sure how to debug this and so I’m hoping I can find some help here.
I’m running WAMP, and the php.ini contains the following:
max_execution_time = 0 ; Maximum execution time of each script, in seconds
max_input_time = 0 ; Maximum amount of time each script may spend parsing request data
Through testing, I have discovered the following :
The script can do exactly 35 iterations through the loop in under 2 minutes.
Any number greater than this causes the error.
If I skip the first 20 curl submits, I can then do 21 to 55 iterations before failure, so I do not believe it is the 36th value.
If I put a sleep(5) command into the loop, I can still do 35 and no more iterations than that.
If I put a sleep(10) command into the loop, it fails on 35 and needs less.
$action = "post"; $url = "http://url.url.url/api/v1.0/udo_Position/create"; // I've replaced the actual URL out of necessity // set user agent { print("Setting CURL options, using URL ".$url."<br>"); $cd = curl_init(); curl_setopt($cd, CURLOPT_HEADER, 1); curl_setopt($cd, CURLOPT_URL, $url); curl_setopt($cd, CURLOPT_USERPWD, 'username:password'); curl_setopt($cd, CURLOPT_RETURNTRANSFER, 1); // Don't send return value to screen curl_setopt($cd, CURLOPT_USERAGENT, "Firefox/2.0.0.1"); // spoofing FireFox 2.0 curl_setopt($cd, CURLOPT_VERBOSE, true); // } if ($action == 'get') { ; } elseif ($action == 'post') { print("POST set;<br>"); print("<pre style='font-family:verdana;font-size:13'>"); print_r($pointval); print("</pre>"); $pointval["save"] = "Save"; // The AddNew step form requires this. curl_setopt($cd, CURLOPT_POSTFIELDS, $pointval); } else { print("Invalid usage. Please see example<br>"); return; } $reply = curl_exec($cd); $http_status = curl_getinfo($cd, CURLINFO_HTTP_CODE); print("<pre style='font-family:verdana;font-size:13'>"); print_r($http_status); print("</pre>"); print("Here is the reply from the AddNew function: <br>"); print("<pre style='font-family:verdana;font-size:13'>"); print_r($reply); print("</pre>"); if (curl_error($cd)) { print("Error: ".(curl_error($cd))."<br>"); } curl_close($cd); print("Insert Successful.<br>");
I am uncertain whether it is a PHP, Apache or cURL issue at this point. I have another script that pushes a great deal more cURL calls to the same system but pulls from only one external resource, instead of two. Any help or suggestions would be appreciated.
Question
If i run this code in PHP with a wrong proxy I get 500 internal server error.
How to handle it and continue execution?
$opts = array(
'http'=>array(
'method'=>"GET",
'proxy' => 'tcp://100.100.100.100:80' //a wrong proxy
)
);
$context = stream_context_create($opts);
$file = file_get_contents('http://ifconfig.me/ip', false, $context);
Answer
I assume you mean two things when you say âhandleâ:
- If the script connects to a âwrongâ proxy, it will wait for a long time to establish the connection, until it times out. The script should set a lower timeout, so users don’t wait forever.
- If an error occures during accessing the external ressource, don’t die or show ugly messages. Instead, pretend everything’s cool.
As for 1) The timeout of a remote connection is defined in PHP’s default_socket_timeout
setting and is by default 60 seconds. You can/should set a much lower timeout for your own calls:
$opts = array(
'http'=>array(
'timeout'=> 2, // timeout of 2 seconds
'proxy' => 'tcp://100.100.100.100:80' //a wrong proxy
)
);
As for 2), you would normally use a try
/catch
block. Unfortunately, file_get_contents()
is one of those old PHP functions that don’t throw catchable exceptions.
You can supress a possible error message by prefixing the function call with the @
sign:
$file = @file_get_contents('http://ifconfig.me/ip', false, $context);
But then you can’t handle any errors at all.
If you want to have at least some error handling, you should use cURL. Unfortunately, it also doesn’t throw exceptions. However, if a cURL error occurs, you can read it with curl_errno()
/curl_error()
.
Here’s your code implemented with cURL:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://ifconfig.me/ip");
curl_setopt($ch, CURLOPT_PROXY, 'tcp://100.100.100.100:80');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 2);
curl_setopt($ch, CURLOPT_HEADER, 1);
$data = curl_exec($ch);
$error = curl_errno($ch) ? curl_error($ch) : '';
curl_close($ch);
print_r($error ? $error : $data);
This way, you can decide what you want to do in the case of an error.
cc by-sa 3.0