I would like to send XML data to a remote URL. How to do this in a proper way using Symfony2?
Equivalent in flat PHP using curl would be:
$ch = curl_init("http://website");
$request["queue"] = file_get_contents("file_to_send.xml");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
$response = curl_exec($ch);
curl_close ($ch);
Well, from here it looks like a good way to do it. Don't forget, keep it simple.
You might want to wrap it in some class that would manager communication with other servers. This way it would be more testable.
Related
I'm a bit confused about API vs WebService. But anyway, which one will you follow to.. lets say to provide simple string or xml or json data return.
Currently i have implemented something like:
Server:
echo json_encode("hello, ".$_POST["q"]);
Client:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $server_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, array('q' => 'world!'));
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER , 1);
$response = curl_exec($ch);
curl_close($ch);
Normally as the standard way, how do we even call/name it? Shall i name it API? Or WebService?
And in normal way (whenever we need to provide some data out of Server, like above) which one do we implement?
I have read that CURL is way too fast than File Get Contents and less memory consuming. So, I will go with CURL. I read some articles to find info about it and how to use it properly and efficiently. The problem is that I found many ways of using CURL, I posted 3 variations below.
My question is which one is the best to use?
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,2);
$content = curl_exec($ch);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $path);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$cache = curl_exec($ch);
curl_close($ch);
$ch = curl_init("");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close ($ch);
It all depends on what options you want. Most of cURL's options will have some default, which will be set if you do not manually set an option.
For example: CURLOPT_RETURNTRANSFER defaults to FALSE (so setting it to FALSE in your code would be redundant).
Passing a parameter to curl_int is just a shortcut for CURLOPT_URL. The other options all depend on what you want to do.
Here is a list of all the cURL options: http://php.net/manual/en/function.curl-setopt.php
Pick the options you want, exclude those you don't. There's no "right" way, just the way that works the way you want.
Its like asking,
I have see many ways people code PHP, but which one is right?
There is no correct answer for such question, as everybody is going to have their own opinion about every way.
So, I suggest you read the PHP.net's manual on CURL and choose the correct method based on your coding style, requirement and construct.
1 is SSL (https:// or ftps://)
2 I think is a plain old ftp:// or http:// url
3 I am not sure what it is. looks like generic URL stuff without SSL.
did you check the notes in the example and read the documentation to see what exactly the functions do or the code does? you can feed curl_init a URL string.
get the .chm documentation with notes. it will help a lot and speed up your research.
looks like you need to have to put in some code that does this:
<?php
function getfile($url) {
$result="";
$protocol=substr($url,0,strstr($url,":"))
if ("https"==$protocol || "ftps"==$protocol) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,2);
$content = curl_exec($ch);
} else {
//it's http or ftp
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close ($ch);
}
return $result;
}
?>
Alright, for a project, there's an XML page, which uses the user's cookies to generate output.
I don't have access to this page, but wish to extract information from it using PHP.
My thoughts so far were some sort of PHP include that sets cookies to the externally included file.
Any pointers or suggestions would be great. Thanks.
EDIT: I have no control over the domain that the XML page is hosted on.
You could use cURL and include a cookie jar:
From http://curl.haxx.se/libcurl/php/examples/cookiejar.html:
<?php
/*
This script is an example of using curl in php to log into on one page and
then get another page passing all cookies from the first page along with you.
If this script was a bit more advanced it might trick the server into
thinking its netscape and even pass a fake referer, yo look like it surfed
from a local page.
*/
$ch = curl_init();
curl_setopt($ch, CURLOPT_COOKIEJAR, "/tmp/cookieFileName");
curl_setopt($ch, CURLOPT_URL,"http://www.myterminal.com/checkpwd.asp");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "UserID=username&password=passwd");
ob_start(); // prevent any output
curl_exec ($ch); // execute the curl command
ob_end_clean(); // stop preventing output
curl_close ($ch);
unset($ch);
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_COOKIEFILE, "/tmp/cookieFileName");
curl_setopt($ch, CURLOPT_URL,"http://www.myterminal.com/list.asp");
$buf2 = curl_exec ($ch);
curl_close ($ch);
echo "<PRE>".htmlentities($buf2);
?>
I use the following command in some old scripts:
curl -Lk "https:www.example.com/stuff/api.php?"
I then record the header into a variable and make comparisons and so forth. What I would really like to do is convert the process to PHP. I have enabled curl, openssl, and believe I have everything ready.
What I cannot seem to find is a handy translation to convert that command line syntax to the equivalent commands in PHP.
I suspect something in the order of :
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
// What goes here so that I just get the Location and nothing else?
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Get the response and close the channel.
$response = curl_exec($ch);
curl_close($ch);
The goal being $response = the data from the api “OK=1&ect”
Thank you
I'm a little confused by your comment:
// What goes here so that I just get the Location and nothing else?
Anyway, if you want to obtain the response body from the remote server, use:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
If you want to get the headers in the response (i.e.: what your comment might be referring to):
curl_setopt($ch, CURLOPT_HEADER, 1);
If your problem is that there is a redirection between the initial call and the response, use:
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
I have one problem regarding CURL.
I am trying send CURL request to http://whatismyipaddress.com
so..
is there any way to get curl response in array ?
because right now it display the HTML Page but i want the response in array.
here is my code...
$ipAdd = '121.101.152.170';
$ch = curl_init("http://whatismyipaddress.com/ip/".$ipAdd);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION ,1);
curl_setopt($ch, CURLOPT_HEADER,0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
echo $output;
So.. currently i am getting the detail HTML page but i want the out put as array or XML.
The "easiest" path is going to be to find the surrounding text & extract based on that.
If you're willing to step your dedication to this up, you can use something like http://simplehtmldom.sourceforge.net/ & go from there.
edit: actually you can use this (it is built into php5) - http://php.net/manual/en/book.dom.php
more specifically, this - http://www.php.net/manual/en/domdocument.loadhtml.php