import JSON file to SOLR with PHP - php

I need to import a JSON file to a (local) SOLR host with PHP.
I'm trying to use the php cUrl to send something but I'm not that well in php.
My Code for sending a request to get a result from solr already worked:
// ...
$url = 'http://localhost:8983/solr/select?q="test"~1&wt=json&indent=true&start=0&rows=2000';
$curl = curl_init();
// set cUrl options
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => array("Content-type:application/json"),
CURLOPT_URL => $url,
CURLOPT_USERPWD => "$login:password"
));
$contents = curl_exec($curl);
$data = json_decode($contents, true);
$docs = $data['response']['docs'];
// ...
But how can I import/index a JSON file to my solr host with cUrl?

Related

Why does this PHP cUrl not apply the correct authentication header?

I am trying to use the files.com API here. I cannot get it to authenticate. The response I keep getting is 'bool(false)'
I need to add an api key to my header. Using PHP I have attempted this:
<?php
$key = '1234567890';
$domain = 'myDomain';
$url ="https://$domain.files.com/api/rest/v1/users.json";
$auth=['X-FilesAPI-Key'=> $key];
$ch = curl_init();
curl_setopt_array($ch,[
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_HTTPHEADER => $auth,
]);
$content = curl_exec($ch);
$info = curl_getinfo($ch);
any suggestions to get this working would be greatly appreciated!

curl_setopt_array for USPTO records returns "Internal Sever Error1"

I'm trying to test retrieving an USPTO dataset for a number of records for oa_rejections using their open API:
https://developer.uspto.gov/ds-api-docs/index.html?url=https%3A//developer.uspto.gov/ds-api/swagger/docs/oa_rejections.json/v2#!/oa_rejections/perform_search
I have made the following php script. It ends up displaying just "Internal Server Error1":
<?
$params=['criteria'=>'*%3A*', 'start'=>'100', 'rows'=>'3'];
$defaults = array(
CURLOPT_URL => 'https://developer.uspto.gov/ds-api/oa_rejections/v2/records',
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $params,
);
$ch = curl_init();
curl_setopt_array($ch, $defaults);
$contents = curl_exec($ch);
echo $contents;
?>
Is there anything vital or obvious that I'm missing, or perhaps I cannot use this syntax at all to retrieve this data?

Cannot remote upload through openload API [PHP - curl]

I'm trying to upload a file using the openload API that can be found here.
When I upload some file I always get a JSON response of success (code: 200):
Unfortunately when I check the file status (using both API and website) it tells me that there was an error in upload. Here the JSON response (code 200):
Here is how I send the request. Where's the error? I tried using both the urlencode function and not.
$user = "...";
$psw = "...";
$link = urlencode("https://google.com/favicon.ico");
$url = "https://api.openload.co/1/remotedl/add?login=$user&key=$psw&url=$link";
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_URL => $url,
CURLOPT_USERAGENT => 'Codular Sample cURL Request'
));
$resp = curl_exec($curl);
curl_close($curl);
echo $resp;
EDIT: Switched to https and added CURLOPT_FOLLOWLOCATION => true, still no success.
I think you are hitting a http 301 redirect while using curl
http://google.com/favicon.ico
to
https://www.google.com/favicon.ico
If that case curl option CURLOPT_FOLLOWLOCATION might help.

Unexpected JSON output on HTTP request

I am currently trying to get a simple HTTP JSON response from a website.
When I look at the page when I use Google Chrome:
{"columns":[{"name":"xx","dataType":"varchar","size":255,"nullable":true},{"name":"xx","dataType":"varchar","size":255,"nullable":true},{"name":"xx","dataType":"decimal","size":17,"nullable":true},{"name":"xx","dataType":"varchar","size":255,"nullable":true},{"name":"xx","dataType":"varchar","size":4,"nullable":true},{"name":"xx","dataType":"varchar","size":2,"nullable":true},{"name":"xx","dataType":"varchar","size":20,"nullable":true}],"rows":[["xxxxx","xxxx/xxxx","xxxx","Yacouba","5xx","xx","xxxxx"]]}
However when I use the following php code:
<?php
$json_url = "xxxx"; // url is something else but privacy reasons etc
// Initializing curl
$ch = curl_init();
// Configuring curl options
$options = array(
CURLOPT_URL => $json_url,
CURLOPT_POST => FALSE
);
// Setting curl options
curl_setopt_array($ch, $options);
// Getting results
$result = curl_exec($ch); // Getting jSON result string
curl_close($ch);
json_decode($result);
var_dump($result);
and then the output is as fallows:
‹ì½`I–%&/mÊ{JõJ×àt¡€`$Ø#ìÁˆÍæ’ìiG#)«*ÊeVe]f#Ìí¼÷Þ{ï½÷Þ{ï½÷º;N'÷ßÿ?\fdlöÎJÚÉž!€ªÈ?~|?"~ñGÓª\/–ÍG¾÷‹?Zf‹ü£Gͪ¦)òú÷/³I^~4úh–µÙ›ë¾ºÌêé<«éæø}°wÿþè£åº¤¦%ýÙÖëü—Œz€–Y¶ø8—U=ËëbyñûÏòIÑæëú÷¯Vù²i³l9Ïòi±ÈJx÷Á&ü´E°iëŒÞÕyóû¯¦¿ÿ²n6CÛ¿=¬²½ ØÞmFXçeÖ9¡v°´ï>ª«+pÈ÷>ÚýôÓ{Ôúm±l&y}‘/ïþ>Ù´ZO2úpoow´»C¿¹ÏîïìJ?žŸÐ?;;;û÷÷?Ýûèûßÿ%ÿOÿÿzbool(true)
What could the problem be?
I fix it! :D with:
CURLOPT_ENCODING => ''

Can a PHP script reuse the same variable?

I am trying to learn PHP coming from some very basic Java experience but I am confused about variables in PHP and being reused in the same script. I have the below code which works without issue somehow even though I have multiple values for certain variables such as $url,$curl,$resp how is this working? Is it just being over written as the script executes?
<?php
$zip = $_GET["ziphtml"];
$url = "http://api.wunderground.com/api/4339efkeyf17a9/forecast10day/q/19115.json";
$curl = curl_init();
$curl = curl_init();
// Set some options - we are passing in a useragent too here
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $url,
CURLOPT_USERAGENT => 'Codular Sample cURL Request'
));
// Send the request & save response to $resp
$resp = curl_exec($curl);
// Close request to clear up some resources
curl_close($curl);
$json_string = $resp;
$parsed_json = json_decode($json_string);
$forecastp2 = $parsed_json->{'forecast'}->{'txt_forecast'}->{'forecastday'}[1]->{'title'};
$forecastp3 = $parsed_json->{'forecast'}->{'txt_forecast'}->{'forecastday'}[2]->{'title'};
$forecastp4 = $parsed_json->{'forecast'}->{'txt_forecast'}->{'forecastday'}[3]->{'title'};
$forecastp5 = $parsed_json->{'forecast'}->{'txt_forecast'}->{'forecastday'}[4]->{'title'};
$forecastp6 = $parsed_json->{'forecast'}->{'txt_forecast'}->{'forecastday'}[5]->{'title'};
$forecastp7 = $parsed_json->{'forecast'}->{'txt_forecast'}->{'forecastday'}[6]->{'title'};
$zip = $_GET["ziphtml"];
$url = "http://api.wunderground.com/api/4dgg345353vdryteyfg339ekey7a9/geolookup/conditions/q/IA/".$zip.".json";
$curl = curl_init();
$curl = curl_init();
// Set some options - we are passing in a useragent too here
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $url,
CURLOPT_USERAGENT => 'Codular Sample cURL Request'
));
// Send the request & save response to $resp
$resp = curl_exec($curl);
// Close request to clear up some resources
curl_close($curl);
$json_string = $resp;
$parsed_json = json_decode($json_string);
$location = $parsed_json->{'current_observation'}->{'display_location'}->{'city'};
$temp_f = $parsed_json->{'current_observation'}->{'temp_f'};
echo "Current temperatdure in ${location} is: ${temp_f}\n<br /> and it is now ${forecastp2}\n<br /> and then it will be ${forecastp3}\n<br /> and then ${forecastp4}\n<br />and then ${forecastp5}\n<br />and then ${forecastp6}\n<br />and then ${forecastp7}";
?>
It is just overwritten.
Java does the same, but Java checks type, so you cannot reassign values as freely.
A variable is a name for a location in memory. When you store a new value in that location, the old value is lost and the name now refers to the new value.

Categories