views:

33

answers:

1

So I'm a beginner to all of this. I'm attempting to teach myself how to properly use CURL, and dabble a bit in API calls (don't worry, this question only relates to using CURL).

Now my original code worked just fine - I structured the request using urlencode, and the vast majority of requests leaded to the responses I was looking for. About 10% of the received responses, however, that wouldn't retrieve the results I needed. Let me give an example:

$urlToFetch = "http://lsapi.seomoz.com/linkscape/url-metrics/" . urlencode($objectURL) . "?" . $this->structureURL();
$response = ConnectionUtil::makeRequest($urlToFetch);

Using the above code would generate a $urlToFetch with the following data: http://lsapi.seomoz.com/linkscape/url-metrics/www.alinki.com%2Fdomainlinks.php%3Fpage%3D516000?AccessID=XXXXXX&Expires=1286388878&Signature=YYYYYYYYY

This passes through CURL, and gets a response from the seoMoz API - the response, however, is missing some data. I've poked around, and found that it is an issue with how the URL is structured. For example, by changed the value from www.alinki.com%2Fdomainlinks.php%3Fpage%3D516000 to www.alinki.com/domainlinks.php?page=516000 I can retrieve all the fields I am looking for. Going by that logic, I should be able to structure a URL similar to this: (I'm removing the http since I cannot post more than one link) lsapi.seomoz.com/linkscape/url-metrics/www.alinki.com/domainlinks.php?page=516000?AccessID=XXXXXX&Expires=1286388878&Signature=YYYYYYYYY and retrieve all the fields I need. The problem is, when I attempt to pass that URL through to CURL, it will not complete the request. Below is the code used for the CURL request:

public static function makeRequest($urlToFetch)
{
$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_URL, "$urlToFetch");
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, ConnectionUtil::CURL_CONNECTION_TIMEOUT);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);

$buffer = curl_exec($curl_handle);
//var_dump($buffer);
curl_close($curl_handle);

$arr = json_decode($buffer);

return $arr;
}

And I request as follows:

 $urlToFetch = "http://lsapi.seomoz.com/linkscape/url-metrics/" . urlencode($objectURL) . "?" . $this->structureURL();
    $response = ConnectionUtil::makeRequest($urlToFetch);

Any ideas on what silly mistake I am making?

A: 

Try using http_build_query instead of urlencode for generating your query string. It looks like the seomoz code is not handling the urlencoded data properly.

bradym