Forms with php/curl
Posted by: MajusC00L
Posted on: 2008-04-19 16:45:00
Hello
I've spent the last 48 hours initiating myself with php/curl after discovering that the fopen() function was not allowed on DH shared servers.
My need is quite simple and I think I managed the hardest part. But stupidly, I'm blocked with something that is probably dead simple and maybe someone here can help.
A web site authorized me tu use their API and search their image database from my own web site. They only have a 1 page documentation showing a URL request lookalike and a description of the XML elements received when the request is successful.
I'm at the point where I managed to create a php file which fetch an example request (always the same) and parse it in a straight and simple table with SimpleXML.
You can check it here: http://fotonumerix.com/api-dreamstime/dreamstimeapi30.php
The code of my script is the following:
------------------------------------------------------------
<?php
function My_simplexml_load_file($URL)
{
$ch = curl_init($URL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$xml = simplexml_load_string(curl_exec($ch));
curl_close($ch);
return $xml;
}
$xml = My_simplexml_load_file('http://www.dreamstime.com/api.xml?username=MYUSERNAME&password=MYENCRYPTEDPASSWORD&type=get&request=search&orderby=downloads&srh_field=EXAMPLEKEYWORD');
if (empty($xml))
{
print "Sorry , I could not find images matching your query.<br />
Verify or modify your keywords.";
}
else
{
print "<table cellspacing=5 cellpadding=5>
<caption align=top><h4>$xml->totalResults search results for your query<br />
$xml->partialResults are shown on this page</h4></caption>";
foreach ($xml->items->item as $item){
print "
<tr><th align=center valign=middle>Dreamstime Image N° <a href=\"$item->imageURL\" alt=\"Image labeled $item->title on Dreamstime\" target=\"_blank\">$item->imageID
</a>
<b>Label</b>: $item->title
<i>Purchased $item->downloads times</i>
</th>
<td align=center valign=middle><a href=\"$item->imageURL\" alt=\"Image labeled $item->title on Dreamstime\" target=\"_blank\"><img src=\"$item->mediumThumb\" title =\"Image labeled $item->title on Dreamstime\"></a></TD></tr>";
}
print "</table>";
}
?>
------------------------------------------------------------
So my problem with all that is to understand the logic of the form I want to create and how I pass the "post" values to this fetchparser script I did (and how i should modify it).
1- Do I have to crate a stand alone php which will contain only the <form> code?
2- Do I have to set <form action="fetchparser.php"> instead of the website?
That is how I understand the concept but if I'm right, I don't know, and I can't find how to transfer the content of the text field (containing the keywords) into the $url I used to fetxh the xml page with php/curl.
I hope my post was not as confused as I am.
Thanks in advance
Friendly
MajusC00L