I have a host for Elasticsearch set up like this 199.55.56.289:9998.
What i need is to make a post with Json body but to also add several custom search query's on that host URL so it looks something like this:
199.55.56.289:9998/archive/blog?param1=0¶m2=150¶m3=0
Is there a way to add those 3 query params in json body when making a post request?
in this case i know that
$query =
[
index: archive,
type: blog
...
]
but what can i do with those uri params?
Elasticsearch supports uri search, So you can use it like this
<?php
$query = 'event:played AND a:b AND #timestamp:c';
$finalUrl = '199.55.56.289:9998/archive/blog?q='.urlencode($query);
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$finalUrl);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
$output=curl_exec($ch);
curl_close($ch);
echo $output.PHP_EOL;
Check Here: https://www.elastic.co/guide/en/elasticsearch/reference/7.9/search-search.html#search-api-query-params-q
Related
I have a search index on my Cloudant that I query using AngularJS and PHP.
So far I'm not getting specific enough results.
For instance, on a search with fair:'Fair 2017', I'm getting all the results that include Fair, including Fair 2016 and so on.
I've tried different search types (simple, standard, classic), and it happens with all of them.
A typical object:
doc:Object
exhibitortype:"Project Space"
fair:"Fair 2017"
...
Here's my AngularJS code:
$scope.loadexhibitors = function(fair){
$scope.searchindex = fair.doc.fairname;
var $promisefairexh=$http({
url: 'databaseconnect/getexhibitors.php',
method: "GET",
params: {search: $scope.searchindex}
});
...
The PHP bit looks like this:
<?php
$search = $_GET["search"];
$newsearch = str_replace(' ', '+', $search);
$url = "https://user:pass.#user.cloudant.com/db/_design/fairs/_search/by_fair?q='$newsearch'&include_docs=true";
$ch = curl_init(); // initialize curl handle
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
$output = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
?>
And my Cloudant search function:
function (doc) {
index("default", doc.fair);
}
On the other hand, on the Cloudant User Interface, when I test the search index and include double quotes on the search input (for example: "Fair 2016" instead of Fair 2016), I get the desired results.
Any tips?
Try using double quotes in your search instead of single quotes, for example:
$url = "https://user:pass.#user.cloudant.com/db/_design/fairs/_search/by_fair?q=\"$newsearch\"&include_docs=true";
Note the change to the q param:
q=\"$newsearch\"
I want to create a POST method form that sends details to a PHP script on another server (ie, not its localhost). Is this even possible? I imagine GET is fine, so is POST possible?
<form method="POST" action="http://the.other.server.com/script.php">
If you want to do that on your server (i.e. you want your server to act as a proxy) you can use cURL for that.
//extract data from the post
extract($_POST);
//set POST variables
$url = 'http://domain.com/get-post.php';
$fields_string = "";
$fields = array(
'lname'=>urlencode($last_name), // Assuming there was something like $_POST[last_name]
'fname'=>urlencode($first_name)
);
//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
$fields_string = rtrim($fields_string,'&');
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
However if you just simply want to send a POST request to another server, you can just change the action attribute:
<form action="http://some-other-server.com" method="POST">
There is another question on Stack Overflow that shows a better way to url-ify the variables. It is better because the method shown in the answer above breaks when you use nested associative arrays (aka hashes).
How do I use arrays in cURL POST requests
If you are really wanting to build that query string manually, you can. However, http_build_query will make your "url-ify the data for the POST" section unnecessary. – Benjamin Powers Nov 28 '12 at 2:48
I'm doing something a bit strange here, I'm querying data out of my local database and posintg it to Salesforce form using cURL. The data posts correctly to Salesforce. However, the select multiple is not getting the correct values selected. See $sd["location"] below in my code:
//init curl
$ch = curl_init();
//setup the params
$url = 'https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8';
$oid = "HIDDEN";
//setup the superdelegate array
$sd = array();
$sd["oid"] = $oid;
$sd["retURL"] = "";
$sd["first_name"] = "1144asdfsadf4";
$sd["last_name"] = "SDFSD1111";
$sd["state"] = "IL";
$sd["location"] = '"Chicago","New York","California"'; //this is the value that submits to the select multiple
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($sd));
//post to Salesforce and then close the connection
curl_exec($ch);
curl_close($ch);
The select multiple is already setup in Salesforce with a 30 different locations. I'm trying to pass the cities that should be selected (Chicago, New York, California). Can you help me fix my code to get this to work correctly?
What you are trying to implement/duplicate is the non-standard (or "experimental" if you prefer) application/x-www-form-urlencoded format. The "x-" in the mime type denotes the fact that it is has not been officially standardized.
The most common application/x-www-form-urlencoded format I've encountered is as specified in the HTML 4 (and probably HTML 5) specification here. Which, when simplified, states that each successful control is paired with it's name and current value, in the order it appears in the form, and separated by the ampersand, "&". This would translate into a HTTP GET or POST method containing something like:
name=test&desc=some%20description&option=1&option=2&option=3
You should also use PHP's urlencode function on form names and values before passing it along in the appropriate location of the HTTP request in order to conform with RFC1738.
As a side note, PHP, though probably not alone, is an odd-ball in how it handles multiple values on a single post var. It follows the HTML suggested specification, but extends it to only consider a var as multiple if its name contains the PHP array append operator, e.g., formname[]. If you try to follow the HTML suggestion (see below) on a PHP script, it will overwrite the previous value on the name when accessing via the $_POST or $_GET superglobals.
I figured it out. This ended up doing it:
Sending as "value1;value2" doesn't work. i.e. implode(";", $array) or join() doesn't work.
Had to split the values like this in the POST string, oddly:
$post_string .= "&the_field=value1
$the_field=value2
$the_field=value3";
Now works fine.
$sd['location'] should be an array if its a multiple. For example:
$location = str_replace('"', '', '"Chicago","New York","California"');
$sd['location'] = explode(',', $location);
However you need to make sure you are not confusing the values with the labels. Salesforce will be expecting the value not the label so if salesforce is built with something like:
1 = Antwerp
2 = California
...
29 = Whatever
Then you need to pass the key values not the actual city names. I dunno what your salesforce stuff looks like so youll have to figure that out :-)
I'm also trying to send multiple select values to Salesforce, but my code looks slightly different and I'm not sure how to implement your solution. It's literally the only solution I can find on the net, so I'm hoping you can help me out. This is what my fields array looks like:
//set POST variables
$url = 'https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8';
$fields = array(
'oid'=>urlencode($oid),
'00NE0000003yZ9B'=>urlencode($Lead_Source_Email),
'00NE0000004hd4z'=>urlencode($Personal_Challenges),
'recordType'=>urlencode($recordType),
'retURL'=>urlencode($retURL),
'first_name'=>urlencode($first_name),
'last_name'=>urlencode($last_name),
'email'=>urlencode($email),
'phone'=>urlencode($phone),
//'00NE0000003uZ4G'=>urlencode($Contact_By),
'00NE0000003uZ9X'=>urlencode($Message)
);
$Personal_Challenges is the field from the select box with the multiple values. I honestly don't understand all of the code, but this is what follows:
//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string,'&');
//print_r($fields_string);
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
I'd love any help you can give me! Sorry to ask this question in an answer, but apparently I can't directly message on StackOverflow.
Cheers!
I see this post is too old, but just in case:
we have to prepare string for it
curl_setopt($ch, CURLOPT_POSTFIELDS, $str_fields);
We can prepare array like:
$fields = array('f1' => 'v1', f2 =>['v21', 'v22', 'v23'], 'f3' =>'v3' );
$fields = http_build_query($fields);
it gives : f1=v1&f2%5B0%5D=v21&f2%5B1%5D=v22&f2%5B2%5D=v23&f3=v3
we need to clear chars like "%5B0%5D"
$str_fields=preg_replace("/(%5[A-F]\d{0,})/" ,"",$fields);
We get f1=v1&f2=v21&f2=v22&f2=v23&f3=v3
Thats all.
Hi I am trying to identify key Twitter influencers for a client and I have a list of 170 twitter id's that I need to learn more about.
I would like the script to loop through the list of Twitter Id's and save the output to a single XML file -
http://twitter.com/users/show/mattmuller.xml
http://twitter.com/users/show/welovecrowds.xml
http://twitter.com/users/show/jlyon.xml
etc
In essence I need to write a script that passes each url and saves the output as a single xml file on the server. Any ideas on how to do this with PHP and do I need to use Curl?
Thanks for any help.
Cheers
Jonathan
This is a simple example of how could you achieve this using cURL:
// array of twitter accounts
$ids = array('mattmuller', 'welovecrowds', 'jlyon' /* (...) */);
$ch = curl_init();
$url = 'http://twitter.com/users/show/';
$xml = '<?xml version ="1.0" encoding="utf-8"?>';
// make curl return the contents instead of outputting them
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
foreach($ids as $id) {
// set the url base on the account id
curl_setopt($ch, CURLOPT_URL, "$url$id.xml");
// fetch the url contents and remove the xml heading
$xml .= preg_replace ('/\<\?xml .*\?\>/i', '', curl_exec($ch));
}
// save the contents of $xml into a file
file_put_contents('users.xml', $xml);
How can I query a particular website with some fields and get the results to my webpage using php?
let say website xyz.com will give you the name of the city if you give them the zipcode. How can I acehive this easliy in php? any code snap shot will be great.
If I understand what you mean (You want to submit a query to a site and get the result back for processing and such?), you can use cURL.
Here is an example:
<?php
// create curl resource
$ch = curl_init();
// set url
curl_setopt($ch, CURLOPT_URL, "example.com");
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// $output contains the output string
$output = curl_exec($ch);
// close curl resource to free up system resources
curl_close($ch);
?>
You can grab the Lat/Long from this site with some regexp like this:
if ( preg_match_all( "#<td>\s+-?(\d+\.\d+)\s+</td>#", $output, $coords ) ) {
list( $lat, $long ) = $coords[1];
echo "Latitude: $lat\nLongitude: $long\n";
}
Just put that after the curl_close() function.
That will return something like this (numbers changed):
Latitude: 53.5100
Longitude: 60.2200
You can use file_get_contents (and other similar fopen-class functions) to do this:
$result = file_get_contents("http://other-site.com/query?variable=value");
Do you mean something like:
include 'http://www.google.com?q=myquery'; ? or which fields do you want to get?
Can you be a bit more specific pls :)
If you want to import the html to your page and analyze it, you probably want to use cURL.
You have to have the extensions loaded to your page (it's usually part of PHP _ I think it has to be compiled in? The manual can answer that)
Here is a curl function. Set up your url like
$param='fribby';
$param2='snips';
$url="www.example.com?data=$param&data2=$param2";
function curl_page($url)
{
$response =false;
$ch = curl_init($url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_FAILONERROR,true);
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch,CURLOPT_TIMEOUT,30);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
$page_data=curl_page($url);
Then, you can get data out of the page using the DOM parsing or grep/sed/awk type stuff.