executing multiple merge neo4j query via php - neo4j

I was using the neo4j query as below till now but I am wondering if it's making neo4j out of memory as I am not committing anywhere(I guess its auto-commit) .But the query works fine only worried if neo4j will shutdown or slow down due to my query. I really appreciate any help.
sample.php
<?php
if (!empty($array)) {
if( get_magic_quotes_gpc() ) {
$array = stripslashes( $array );
}
$newstr = json_decode( preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $array), true );
if(!empty($newstr)){
$j=0;
foreach($newstr as $item) { //foreach element in $arr
$category_id = $item['category_id']; //etc
$category_name = $item['category_name'];
$category = $item['category'];
$data2 .=' MERGE (u'.$j.':Category {name:"'.$category_name.'",id:"'.$category_id.'",category:"'.$category
.'"}) ';
$j=$j+1;
}
$data2 = array("query" =>$data2);
$data_string = json_encode($data2);
$ch = curl_init('http://localhost:7474/db/data/cypher');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$result = curl_exec($ch);
}else{
echo "null";
}
}
?>

You are using the Legacy Cypher HTTP Endpoint, which automatically commits at the end of a successful query. So, there is no need to worry about losing your data.
However, the legacy endpoint is now deprecated, so you should consider changing over to the Transactional endpoint.
Also, as a suggestion, you can change this snippet:
' MERGE (u'.$j.':Category {name:"'
to:
' MERGE (:Category {name:"'
Your query never uses the uxxx identifiers, so there is no need to define them in the first place.

Related

Sending voice to Telegram through PHP app

Here is the situation.
I have an Android app, which can send voice file with HTTP POST anywhere I specify. Also I would like to download these voice files to Telegram bot automatically. I wrote a PHP bot and I can accept and translate to Telegram any fields that come in said POST. One thing I miss is the file itself.
I don't know how to "say" to telegram API I download a file.
So what do I have: present file in $_FILE stream (I can read the name of it), I throw it in sendVoice function as $voice, also I do know I have to use simple multipart/form-data POST method to send file.
My question is: is it possible to use something but CURL to POST right from PHP? If not, what am I missing then? Other methods I use are messages and photo sending via file_get_contents(), I wonder if it's still suitable for voice file?
Here is the code I have now, which I found somewhere (I have 0 cURL knowledge):
function sendVoice($chat_id, $voice, $caption) {
$boundary = uniqid();
$delimiter = '-------------' . $boundary;
$fields = array(
"chat_id" => $chat_id,
"caption" => $caption
);
$post_data = build_data_files($boundary, $fields, $voice);
$ch = curl_init($GLOBALS['api'].'/sendVoice');
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
//"Authorization: Bearer $TOKEN",
"Content-Type: multipart/form-data; boundary=" . $delimiter,
"Content-Length: " . strlen($post_data))
);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_exec($ch);
curl_close($ch);
}
function build_data_files($boundary, $fields, $files){
$data = '';
$eol = "\r\n";
$delimiter = '-------------' . $boundary;
foreach ($fields as $name => $content) {
$data .= "--" . $delimiter . $eol
. 'Content-Disposition: form-data; name="' . $name . "\"".$eol.$eol
. $content . $eol;
}
foreach ($files as $name => $content) {
$data .= "--" . $delimiter . $eol
. 'Content-Disposition: form-data; name="' . $name . '"; filename="' . $name . '"' . $eol
//. 'Content-Type: image/png'.$eol
. 'Content-Transfer-Encoding: binary'.$eol
;
$data .= $eol;
$data .= $content . $eol;
}
$data .= "--" . $delimiter . "--".$eol;
return $data;
}
This was actally much easier. Since we have file present in $_FILES, we can do all of the cURL in just sendVoice() function without any hassle of rebinding boundaries or specifying content types.
Here is the final code which worked for me:
function sendVoice($chat_id, $voice, $caption) {
$filepath = realpath($_FILES['file']['tmp_name']);
$post_data = array(
'chat_id' => $chat_id,
'voice' => new CURLFile($filepath),
'caption' => $caption
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $GLOBALS['api'].'/sendVoice');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_exec($ch);
curl_close($ch);
}
That will download an .OGG file as voice message (because I do not want any files to appear in my music player). Later it is possible to just add more fields such as duration in $post_data array and Telegram would eat them accordingly.

Can I schedule a Jenkins Build without CRON or REST API?

To schedule a build in Jenkins I need to add a "cron" parameter then all works well. But I have a lot of donkey users and they didn't know how to schedule with cron.
Is there a way to schedule a Jenkins build without the API itself (http://jenkins/job/jobname/build?delay=4000 I don't want this) or cron? Maybe some Jenkins Plugin...
Solved it this way:
<?php
if($_POST) {
$fields = array(
"POST_PARAMETERS" => $_POST['params']
);
$delay = (int) $_POST['delay'];
$username = "my_username";
$password = "my_password";
$token = "MY_JENKINS_TOKEN_NAME";
$job = "JOB_NAME";
$url = "http://jenkins_host/jenkins/job/".$job."/buildWithParameters?token=".$token."&delay=".$delay;
$process = curl_init($url);
curl_setopt($process, CURLOPT_HEADER, 1);
curl_setopt($process, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($process, CURLOPT_TIMEOUT, 30);
curl_setopt($process, CURLOPT_POST, 1);
curl_setopt($process, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($process, CURLOPT_POSTFIELDS, http_build_query($fields));
curl_setopt($process, CURLOPT_POST, count($fields));
$return = curl_exec($process);
echo http_build_query($fields);
echo curl_error($process);
curl_close($process);
die;
}
?>
I give the JOB_NAME and the build parameters and it work well. cURL did the trick for me with the authorization token.
Thanks everyone who tried to help.

Getting the price of Priority Mail Express in USPS shipping

I required to get the rate for standard post,priority mail as well as priority express mail.Using below code i am getting the price rates for priority mail and standard post but not for priority express mail.
When in service column i write the priority express mail then there was an error as given below(Priority mail express)
"The requested Mail Service is not available for the specified request attributes."
Code i am using is this:
{
$url = "http://Production.ShippingAPIs.com/ShippingAPI.dll";
$ch = curl_init();
// set the target url
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
// parameters to post
curl_setopt($ch, CURLOPT_POST, 1);
$data = "API=RateV4&XML=<RateV4Request USERID=\"$userName\"><Package ID=\"1ST\"><Service>All</Service><ZipOrigination>$orig_zip</ZipOrigination><ZipDestination>$dest_zip</ZipDestination><Pounds>$weight</Pounds><Ounces>0</Ounces><Container/><Size>REGULAR</Size><Machinable>FALSE</Machinable></Package>
</RateV4Request>";
// send the POST values to USPS
curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
$result=curl_exec ($ch);
$data = strstr($result, '<?');
//echo '<!-- '. $data. ' -->'; // Uncomment to show XML in comments
$xml_parser = xml_parser_create();
xml_parse_into_struct($xml_parser, $data, $vals, $index);
xml_parser_free($xml_parser);
$params = array();
$level = array();
foreach ($vals as $xml_elem) {
if ($xml_elem['type'] == 'open') {
if (array_key_exists('attributes',$xml_elem)) {
list($level[$xml_elem['level']],$extra) = array_values($xml_elem['attributes']);
} else {
$level[$xml_elem['level']] = $xml_elem['tag'];
}
}
if ($xml_elem['type'] == 'complete') {
$start_level = 1;
$php_stmt = '$params';
while($start_level < $xml_elem['level']) {
$php_stmt .= '[$level['.$start_level.']]';
$start_level++;
}
$php_stmt .= '[$xml_elem[\'tag\']] = $xml_elem[\'value\'];';
eval($php_stmt);
}
}
curl_close($ch);
//print_r($data);exit;
echo '<pre>'; print_r($params); echo'</pre>'; // Uncomment to see the full array
//echo $params['RATEV4RESPONSE']['1ST'][$servicecode]['RATE'];exit;
}
Priority Mail Express is not available for all types of shipments and it might be restricted in this case. Given that your request works for other mailclasses, there's a high likelihood that this is a shipping, not technical issue. It would help if you can share the specific parameters that you're sending.
If you want to play around with USPS shipping rates you can retrieve shipping rates for free with an easy UI or API at https://goshippo.com.

CURL - no url set

$param = 'get.php';
$url = 'http://mysite.org/'. $param;
$__post = 'option=1&option2=225';
mysite.org/get.php - exists for 100%, when I open them in browser everything works.
$c = curl_init();
curl_setopt($c, CURLOPT_URL, $url);
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
curl_setopt($c, CURLOPT_POST, true);
curl_setopt($c, CURLOPT_POSTFIELDS, $__post);
$result = curl_exec($c);
curl_close($c);
print_r($result); // show 404 o_O
I test it using sniffer and CURL pass URL :
http://mysite.org
not
http://mysite.org/get.php
when I do somethink like that :
curl_setopt($c, CURLOPT_URL,'http://mysite.org/get.php');
everything works fine... so... please help me :D What I do wrong, certainly it is a small error but I don't see where he is :/

Decoding the information from token with janrain

I am using janrain widget for OAuth process on my website. I am testing this in following URL: http://vignesh.gvignesh.org/register/
After clicking the register button, the user is signed in from Google or Yahoo or Facebook. I am able to get the token and store it in a variable. I am displaying the token in my site for testing. Now I don't know how to extract the user information from the token I am getting.
<?php
$rpxApiKey = 'YOUR_API_KEY_HERE';
if(isset($_POST['token'])) {
/* STEP 1: Extract token POST parameter */
$token = $_POST['token'];
/* STEP 2: Use the token to make the auth_info API call */
$post_data = array('token' => $_POST['token'],
'apiKey' => $rpxApiKey,
'format' => 'json');
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_URL, 'https://rpxnow.com/api/v2/auth_info');
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$raw_json = curl_exec($curl);
curl_close($curl);
/* STEP 3: Parse the JSON auth_info response */
$auth_info = json_decode($raw_json, true);
if ($auth_info['stat'] == 'ok') {
/* STEP 3 Continued: Extract the 'identifier' from the response */
$profile = $auth_info['profile'];
$identifier = $profile['identifier'];
if (isset($profile['photo'])) {
$photo_url = $profile['photo'];
}
if (isset($profile['displayName'])) {
$name = $profile['displayName'];
}
if (isset($profile['email'])) {
$email = $profile['email'];
}
print $identifier;
echo "</br>";
print $photo_url;
echo "</br>";
print $name;
echo "</br>";
print $email;
/* STEP 4: Use the identifier as the unique key to sign the user into your system.
This will depend on your website implementation, and you should add your own
code here.
*/
/* an error occurred */
} else {
// gracefully handle the error. Hook this into your native error handling system.
echo 'An error occured: ' . $auth_info['err']['msg'];
}
}
?>
This will extract the information from the token which we get after the authentication.

Resources