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?
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.
Related
I have a HTML/PHP/JS page that I use for an automation process.
On load, it performs a curl request like :
function get_data($url) {
$curl = curl_init();
$timeout = 5;
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($curl);
curl_close($curl);
return $data;
}
$html = get_data($url);
Then it uses DOMDocument to retrieve a specific element on the remote page. My PHP code handles it, makes some operations, then stores it in a variable.
My purpose as you can guess is to simulate a "normal" connexion. To do so, I used the Tamper tool to see what requests are performed, when I was physically interacting with the remote page. HTTP headers are made of UA, cookies (among them, a session cookie), and so on. The only POST variable I have to send back is my PHP variable (you know, the one wich was calculated and stored in a PHP var). I also tested the process with Chrome, which allows me to copy/paste requests as curl.
My question is simple : is there a way to handle HTTP requests / cookies in a simple way ? Or do I have to retrieve them, parse them, store them and send them back "one by one" ?
Indeed, a request and a response are slightly different, but in this case they share many things in common. So I wonder if there is a way to explore the remote page as a browser would do, and interact with it, using for instance an extra PHP library.
Or maybe I'm doing it the wrong way and I should use other languages (PERL...) ?
The code shown above does not handle requests and cookies, I've tried but it was a bit too tricky to handle, hence I ask this question here :) I'm not lazy, but I wonder if there is a more simple way to achieve my goal.
Thanks for your advices, sorry for the english
stupid question I think.
I have an API that i want to access. if I simply put the url in my browser it returns all the results correctly.
https://api.mydomain.com/v1/name?user=user1&pass=1234
in my browser this returns array information:
{"firstname":"John","Surname":"Smith"}
I want to be able to use PHP to simply assign the results of the URL page to a variable:
$url="https://api.mydomain.com/v1/name?user=user1&pass=1234";
$result=parse($url);
print_r($result);
This obviously doesnt work but just looking for the correct syntax. done some research but not getting any luck. should be so simple but is not.
advice appreciated as always.
Thanks
Just make a request to your API service:
$url="https://api.mydomain.com/v1/name?user=user1&pass=1234";
$result = file_get_contents($url);
Then, if I understand correctly, your API returns JSON response, so you have to decode it:
$vars = json_decode($result, true);
Which will return an array with all the variables:
echo $vars['firstname']; //"John";
echo $vars['Surname']; //"Smith";
solution: file_get_contents (or maybe you'll need curl if ini_get("allow_url_fopen") !=1 ....)
$url="https://api.mydomain.com/v1/name?user=user1&pass=1234";
$result=parse(file_get_contents($url));
print_r($result);
hope your "parse()" function knows how to parse the result from your api call. i guess you don't know what you're doing, and next you'll be asking why the parse function is not defined :p (it looks like you're looking for json_decode , just a guess.)
i think your parse function would look like:
function parse($apiresponse){return json_decode($apiresponse,true);}
If you have the CURL library installed, you could do this:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.mydomain.com/v1/name?user=user1&pass=1234');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
$vars = json_decode($result, true);
//do something useful with the $vars variable
Cannot you do some search?
Take a look at Google with keywords "PHP get results from URL"
I am building a PHP script that needs to use content loaded from an external webpage, but I don't know how to send/receive data.
The external webpage is http://packer.50x.eu/ .
Basically, I want to send a script (which is manually done in the first form) and receive the output (from the second form).
I want to learn how to do it because it can surely be an useful thing in the future, but I have no clue where to start.
Can anyone help me? Thanks.
You can use curl to receive data from external page. Look this example:
$url = "http://packer.50x.eu/";
$ch = curl_init($url);
// curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // you can use some options for this request
// curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); // or not to use them
// you can set many others options. read about them in manual
$data = curl_exec($ch);
curl_close($ch);
var_dump($data); // < -- here is received page data
curl_setopt manual
Hope, this will help you.
You may want to look at file_get_contents($url) as it is very simple to use, simpler than CURL (more limited though), so your code could look like:
$url = "http://packer.50x.eu/";
$url_content=file_get_contents($url);
echo $url_content;
Look at the documentation as you could use offset and other tricks.
I am trying to access some basic info from an XML based API. I'm new to this and am getting lost reading the guides on php.net and via google that seem to assume I already know the basics.
The input needs to be formatted as follows:
<query>
<auth_key>xxxxx</auth_key>
<command>get_account</command>
<account_id>11122</account_id>
</query>
The return will be in an XML format. I assume I need to use CURL to connect and send the input, but I'm lost - how should the PHP code look to do this?
::UPDATE::
okay, I'm still struggling and not making any progress. I've found a tutorial that has kinda lead me to the following code, but it's not doing anything and I can't figure out how/where I'm supposed to acutally send the XML data through to the url.
$URL = 'http://www.test.com';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $URL);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
curl_close($ch);
print_r($data);
I'm not getting this right and not sure what I'm supposed to be doing. Any help would be appreciated!
The API probably expects you to POST your request to the server, in which case an HTTP POST with cURL example should help. Check the HTTP code of server response (200 is good, others are probably bad) - and if it's good then parse the XML.
Most APIs have very good documentation and examples though. It's worth reading through them or Googling for other people in your shoes.
I ended up stumbling on the following link: http://www.phpmind.com/blog/2009/08/how-to-post-xml-using-curl/
From there I got the sample code working and manipulated it to suit my needs. I also ended up using http://php.net/manual/en/book.simplexml.php for the xml parsing and it seemed very straight forward.
How should I configure cURL to retrieve data from the yahoo maps api?
Here is the current code
curl_setopt($ch, CURLOPT_TIMEOUT, 5000);
curl_setopt($ch, CURLOPT_URL, $geocodeurl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
Which returns a 400 - Bad request HTML file. I've checked $geocodeurl and it is a valid XML file, so I figure the problem must be the cURL options?
$geocodeurl is
http://where.yahooapis.com/geocode?appid=** My App ID **&q=Battle%20Creek,MI&gflags=R
i wrote a simple class wrapper to get a basic address as a php object, which might help you get the job done! i also added a google geocoding wrapper.
to answer your question, everything is ok with the url you posted, but yes as you mentioned on your reply you should urlencode the params
$query = $this->url."?appid=".$this->appid."&flags=".$this->format;
$query .= "&location=".urlencode($address);
here is a link to my wrapper class
https://github.com/mrpollo/Geocoding-API
OK as usual I had misidentified the problem. cURL was fine, but the q= variable was not being passed through any sort of urlencode function correctly.
Worked in my browser because Firefox kindly changed the to %20. With cURL you need to be more careful. . . .