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) );
}