PHP ~ Create a new Thread

Discussion in '[bd] API' started by Sekonda, Aug 25, 2013.

  1. Sekonda

    Sekonda New Member

    #1 , Aug 25, 2013
    Last edited: Aug 25, 2013
    Hi,

    I've been trying for the last week to figure this out, but no luck at all. I've posted my code and output below.
    Code:
    PHP:
    <?php

        
    // Sends CURL Request
        
    function createThread($forum$title$body) {
            
    $ch curl_init();
            
    $data = array(
                
    'oauth_token' => 'REMOVED',
                
    'forum_id' => $forum,
                
    'thread_title' => $title,
                
    'post_body' => $body
            
    );

            
    curl_setopt($chCURLOPT_URL"http://themineville.com/api/threads");
            
    curl_setopt($chCURLOPT_RETURNTRANSFER1);
            
    curl_setopt($chCURLOPT_POST1);
            
    curl_setopt($chCURLOPT_POSTFIELDS$data);

            
    $result curl_exec($ch);
            
    $info curl_getinfo($ch);
            
    var_dump($info);
          
            
    curl_close($ch);
            return 
    $result;      
        }
      
        
    // Builds thread body
        
    function doBan($username,$sender,$reason) {
            
    $date date('H:i @ d/m/Y');
            
    $forum 14;
            
    $body "This is an automated ban report! Originally sent in by [B]@".$sender."[/B]\n\n";
            
    $body .= "[B]Username:[/B] ".$username."\n[B]Time &amp; Date:[/B] ".$date."\n";
            
    $body .= "[B]Temporary:[/B] YES/NO [COLOR=#ff0000]!TODO[/COLOR]\n";
            
    $body .= "[B]Reason:[/B] ".$reason."\n";
            
    $body .= "[B]Proof:[/B] [COLOR=#ff0000]!TODO[/COLOR]";
            
    $body .="\n\n@".$sender." Please update this and then remove these automated message.";

            
    $send createThread($forum$username$body);
        }


        
    $return doBan("Sekonda","Sekonda","Test API");
        
    var_dump($return);
      
    ?>
    Output:
    Edit: When I run the curl command via Terminal I get Empty Response from Server
     
  2. xfrocks

    xfrocks XenForo rocks!
    Staff Member

    You need to return the $send variable in doBan method to get server response for the request.

    What is your curl command btw?
     
  3. Sekonda

    Sekonda New Member

    The curl command is the equivalent to below, where BLAH is $data

    Code:
    curl -XPOST http://themineville.com/api/threads -d 'BLAH'
    I added the return in, but it doesn't show anything because the createThread returns null
     
  4. xfrocks

    xfrocks XenForo rocks!
    Staff Member

    Please send me your token via a conversation, I'll build up curl command for you.
     
  5. xfrocks

    xfrocks XenForo rocks!
    Staff Member

    Thanks for sending me the token.

    However, it looks like your server blocked POST requests targetting /api/, can you check for that? It can be Apache or firewall (iptables)? That's why you get empty response from server.
     
    1. Sekonda

      Sekonda New Member

      #6 Sekonda, Aug 27, 2013
      Last edited: Aug 27, 2013
      Sekonda @xfrocks
      It works if I try to post from Postman, but not using curl. Shall speak to the host now though.
       
    2. xfrocks

      xfrocks XenForo rocks!
      Staff Member

      xfrocks @Sekonda Very likely a firewall issue. Probably mod_security or similar. I'll try again when you sort that out okie?
       
      Digital Doctor likes this.
      1
  6. xfrocks

    xfrocks XenForo rocks!
    Staff Member

  7. Sekonda

    Sekonda New Member

    Hi @xfrocks I spoke to the host, they said nothing is blocked at their end and tested a POST curl request on the same site and it worked fine.
     
  8. xfrocks

    xfrocks XenForo rocks!
    Staff Member

    You have just enabled CloudFlare, right? Because executing `curl -XPOST http://themineville.com/` is now returning CloudFlare error "520: Web server is returning an unknown error"

     
  9. Sekonda

    Sekonda New Member

    It was enabled last night, I disabled it before to see if it was that blocking it.

    However there is a rule for /api/* for it to bypass the cache.
     
  10. xfrocks

    xfrocks XenForo rocks!
    Staff Member

    Executing `curl -XPOST http://themineville.com/api/threads` yelded the same response. Definitely something is blocking it (from the server, not CF)... I tried different user agent with curl but that doesn't work.
     
  11. Sekonda

    Sekonda New Member

    Hi,

    I tested using Postman again and it works fine. Is there an alternative to using CURL?
     
  12. xfrocks

    xfrocks XenForo rocks!
    Staff Member

    For our testing purpose, there are many tools to use. However, for PHP scripts, curl library is probably the best of its kind: easy to use, extremely powerful... Behind the scene, curl command and curl library run the same code so you will not be able to run your script.
     
Loading...