4
votes

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

5
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.Lorenz Meyer
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???David Jones
Does your non-local server have curl lib installed?Artur
Yes it does, I mentioned that in the postDavid Jones
you have die( $result ); before return $result;Aris

5 Answers

6
votes

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

3
votes

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?

1
votes

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://stackguides.com/questions/9957397/tmpfile-returns-false-on-godaddy-server

0
votes

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 ?

0
votes

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!