4

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:

public static function send( $xml )
{
    $xml = str_replace( "\n", "", $xml );

    //Write to temporary file
    $put_data = tmpfile();
    fwrite( $put_data, $xml );  
    fseek( $put_data, 0 );

    $options = array(
            CURLOPT_URL => 'http://*****************/cgi-bin/commctrl.pl?SessionId=' . Xml_helper::generate_session_id() . '&SystemId=live',
            CURLOPT_RETURNTRANSFER => 1,
            CURLOPT_HTTPHEADER => array( 'Content-type: text/xml' ),
            CURLOPT_PUT => TRUE,
                CURLOPT_INFILE => $put_data,
            CURLOPT_INFILESIZE => strlen( $xml )
        );

        $curl = curl_init();
        curl_setopt_array( $curl, $options );
        $result = curl_exec( $curl );
        curl_close( $curl );

        return $result;
    }

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.

Thanks

UPDATE:

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.

ANOTHER UPDATE

I have tried the same script on a different server and heroku and I still get the same result.

ANOTHER UPDATE

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?

FINAL UPDATE

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().

12
  • 3
    The error 500 is generated on the server, here http://************/cgi-bin/commctrl.pl?SessionId=..., it is not generated by curl. What you need to do is to look at the error log on your distant host, not on the one running the curl script. Commented Nov 18, 2013 at 16:56
  • Thanks for the feedback, how comes it works on my localhost though? Wouldn't it return a 500 error no matter where I access it from??? Commented Nov 18, 2013 at 17:00
  • Does your non-local server have curl lib installed? Commented Nov 18, 2013 at 17:03
  • Yes it does, I mentioned that in the post Commented Nov 18, 2013 at 17:05
  • you have die( $result ); before return $result; Commented Nov 18, 2013 at 17:52

5 Answers 5

6
+125

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

More info can be found here http://php.net/manual/en/function.set-time-limit.php

Sign up to request clarification or add additional context in comments.

4 Comments

Hi, I have tried that and it did not work. 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(). Thanks anyway.
So does that mean there is nothing you can do?
Yep, we need to move the site to our dedicated server as the API we are accessing is extremely slow. It takes 49 seconds to get a response and the max execution time is 30 on our shared hosting.
Sucks, what a waste of 250 rep points. At least you got it figured out
3

Here are a few things to try:

  1. 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.

  2. 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.

  3. 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.

  4. 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?

2 Comments

1. The session ID needs to change for each request. Although I did try a few times with it hardcoded and they all failed. 2. I tried this and it returned the expected response. Does this mean the server is the issue? 3. The error log and the access log does not show anything to help us. 4. I have installed wireshark, nice tool tbh. I have checked the response when sending requests from my localhost and they are exected what I expect. Am i able to see what comes back from the server we are having issues on??
1. If the session needs to change with each request, then maybe the problem is that the session id it generates is wrong? 2. If the command line curl works and the php doesn't, then the problem is with the php code; if the command line doesn't work either, then the problem is with the connection between computers. 4. Yes, Wireshark should show packets sent and received, it just can be confusing to configure.
1

I suspect your server does not support tmpfile(). Just to verify:

public static function send( $xml ) {
$xml = str_replace( "\n", "", $xml );

//Write to temporary file
$put_data = tmpfile();
if (!$put_data) die('tmpfile failed');
...

If you are on GoDaddy server check this out... https://stackoverflow.com/questions/9957397/tmpfile-returns-false-on-godaddy-server

1 Comment

Hi thanks for the feedback, I have testing this and found it is not the cause of the issue.
0

Which server is actually showing the 500 ? from your code it seems the local server rather than the remote. change

public static function send( $xml )
{

to

public static function send( $xml )
{
    error_reporting(E_ALL);
    if(!function_exists('curl_exec')) {
        var_dump("NO CURL");
        return false;
     }

does that work ?

20 Comments

For a start you are missing a semi colon on the var_dump and I said that curl is enabled in the post. Thanks.
Yeh the one for our server doesn't indicate a error occurred when submitting data
does the error reporting show any more ? From the code you have posted your never getting the status code from there server. Just the outpu. Is there an error processing the xml ?
No it is the execution of curl that triggers the 500 error. If I remove the curl_exec() call the script works fine. I have error reporting on but it still shows the generic 500 internal error page.
It really sounds like curl exec isnt allowed for some reason. Is php5-curl installed ? is ``curl_exec` in the disable_functions list ? Can you post a php info page ?
|
0

This is almost certainly NOT the php timeout setting. If you are using FastCGI as you have stated then you need to edit this file:

/etc/httpd/conf.d/fcgid.conf

And change:

FcgidIOTimeout 3600

Then do:

service httpd restart

This was driving me insane for 3 days. The top voted answer to this is wrong!

1 Comment

Yes I know its not the PHP timeout setting, it was established somewhere in the comments. I solved it by putting it on a dedicated server where I am able to change the settings.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.