0

I have coded a REST API myself that consumes another REST API.

It often works, but it also often results in 400_Bad_Request.

The flow is this: it works, works, works for so long and then it doesn't work, doesn't work and doesn't work for so long. I then just take a break and after an hour or two, it works again. It doesn't matter what browser I use - chrome or firefox (but I did try clearing the cache). If I enter the URL directly in my browser (that I am trying to consume in my web-service), then I get proper response, so it's not like they block my ip-address for a little while.

    public function apiFunction()
    {

        parse_str($_SERVER["QUERY_STRING"]);
        $host = "THE API ADDRESS"; //omitted due to privacy
        $tran = new apiCallClass( "username", "pwd" ); //omitted due to privacy  
        $tran->setSomeData( $var1, $var2, $var3 ); 
        $tran->setSomeRequest( $var4, $var5 ); 
        $tran->setSomeField("somevar", $var6); 
        $tran->setHost( $host ); 
        $tran->execute(); 
        parse_str($tran->ResponseRaw,$theArr);
        //... other code goes here, 
        //but I can already see ResponseRaw has 400_Bad_Request
    }

This is the function that could be the source of the problem: if I copy the value of $url and paste it in browser, it works, so it must be something that this function is doing that's wrong:

function processRequest( $url ) 
{ 
     $temp = $url;
     $ch = curl_init($url); 
     curl_setopt($ch,CURLOPT_FRESH_CONNECT,TRUE); 
     curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE); 
     curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE); 
     curl_setopt($ch,CURLOPT_HEADER, 0); 
     if ( !$this->isBlank($this->ProxyHost) ) 
     { 
     curl_setopt ($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP); 
     curl_setopt ($ch, CURLOPT_PROXY,$this->ProxyHost); 
     } curl_setopt ($ch, CURLOPT_URL, $url); 
     curl_setopt ($ch, CURLOPT_TIMEOUT, 120); 
     curl_setopt($ch,CURLOPT_RETURNTRANSFER, TRUE); 
     $this->parseResponse( curl_exec($ch) ); 
}
4

1 回答 1

1

如果您发出完全相同的请求并且响应没有返回除 400 状态代码之外的任何消息正文,您将需要联系 API 提供商。

如果您从同一个 IP 地址执行大量请求,我怀疑您正在达到某种速率限制(它们应该返回 429 Too Many Requests 但我已经看到许多 API 返回 400 的速率限制)。

于 2013-04-07T14:08:46.633 回答