file_get_contents not waiting - php

I'm using a PHP call to a REST service to retrieve information from the eXist XML database, running on localhost, capturing the results with file_get_contents(), and using those results to populate a drop-down list in a query form. I thought that file_get_contents() was supposed to wait automatically for complete results, but apparently it doesn't; sometimes the list is fully populated and sometimes it's truncated. The truncation happens at various locations, and reloading the page (rerunning the PHP, and therefore the REST call) usually fixes it, although sometimes not on the first try.
If I've diagnosed the problem correctly as a matter of file_get_contents() not waiting for results, can anyone advise me about how to fix it? Or is there an alternative explanation? Here's the relevant snippet from the PHP:
$getPersonNamesQuery = <<< EOQ1
{for \$i in doc('/db/genealogy/genealogy.xml')//person[not(.//firstName eq "unknown")]
order by string-join(\$i/name/*," ")
return
{normalize-space(concat(
\$i/name/firstName,
" ",
if (\$i/name/epithet) then concat("â",\$i/name/epithet,"â) else "",
" ",
\$i/name/patronymic," ",
if (not(\$i/#origin eq "Rus'" or \$i/#origin eq "unknown")) then concat("of ",\$i/#origin) else ""
))}
}
EOQ1;
$contents = "http://localhost:8080/exist/rest/db/genealogy?_howmany=10000&_wrap=no&_query=" . urlencode($getPersonNamesQuery);
$personNames = file_get_contents($contents);
Thanks,
David

Same thing happened with me, I was using http_build_query to build query string, and the result of file_get_contents and cURL both were responding prematurely. Passing the complete query string without http_build_query in both of the functions resulted in a success. Weird!
This resulted is truncated data:
$pUrl=array('username'=>'_username_',
'variable'=>'value',
'variable2'=>'value2',
'variable3'=>'value3');
$cURL=http_build_query($pUrl);
print_r(file_get_contents("http://www.example.com/api/?$cURL"));
This returned the complete data everytime:
print_r(file_get_contents("http://www.example.com/api/?username=_username_&variable=value&...."));

Related

trouble with double quotes first record of PHP array

php5, Microsoft SQL Server Management Studio, OS 6.3.15063. i get $row data via mssql_fetch_assoc and stuff a PHP array with the $row data just fine when the first record does not have double quotes. subsequent row data can have double quotes, just not the first row. Error: "SyntaxError: missing ) after argument list" in jquery.min - so i cannot peruse and get helpful info from that. code:
$History_SQL = "
DECLARE #AID int;
SET #AID = $Activity_ID;
SELECT activityid, lpid, abstract, changeid, changestamp FROM cpy_activity_history
WHERE activityid = #AID ORDER BY changestamp DESC;
";
$History_results = mssql_query($History_SQL);
while ($row = mssql_fetch_array($History_results)) {
$HistoryRecords[] = $row;
}
This works when record index[0] does not have double quotes, but when double quotes, errors out.
Interestingly, if i use:
$HistoryRecords[] = json_encode($row);
RESULT>>SyntaxError: missing ) after argument list
then it gets past the error; but I am having trouble usind json_decode to get any useful information out of the array [not even getting js braces or anything, getting another record from another table, possibly because this value is not what it thought it was going to be/either way, not sure i am even starting down the right road with json_endode/decode.]
it could be i simply do need help learning how to decode each record coming out of the json encoded var. either way, does this problem look familiar, and any ideas for how to stop getting the error? NOTE: ultimately, i need to display the array information via javascript. other related posts do not seem to cover this scenario. these double quotes are proving maddening. thanks in advance!
thx, All for helping!
the SYMPTOMS included the [a]browser crashing with console/js error: "missing ) after arg list", [b]a var in the code to display an erroneous field, unrelated to the sql used to create info for that var.
the REASON this occurred was that i had within PHP, a console.log that contained a PHP variable. so i was mixing js and PHP. the PHP was firing late and so at the time, the js console.log had nothing to display.
the CLUE which led to fixing this was noticing that the js msg was crashing in the browser, YET the php LOG_IT stmts below the js were working. the js was trying to console.log a msg that had a null value, and somehow a js var was getting loaded with wrong information. sheeze.
the CODE - before the fix the php echo from within the console.log was the culprit. i tossed the original offending stmt, but it was something like this:
<?php here in php land....., now i want to do a console.log....
?><script>console.log("show me info!: " <?php echo $info; ?></script><?php
...back in php land...
i commented out the "echo $info;" and things worked. of course i ripped it out altogether. funny, it would've cleared itself up when i stopped banging my head on the wall and simply decided to clean up and remove all the troubleshooting console.logs.
the CONCLUSION - an old lesson, "don't mix js and php", even in error logging!
thx again for all your help!

Does echo and print not work in the 'live' version of php?

So I'm quite new to programming, and I have nothing else to describe it but a live script, so please correct me with the official term. Anyway, a while ago, I made this bot in php and ran it locally in my browser using xampp on my mac. I could very easily use echo and print_r to print arrays and whatever to the webpage. The script would only run if I reloaded the page, so this is what i'm talking about as 'not live'. Now I have started trying to make a messenger bot in PHP, and i'm using cloud9. I also see the script in a browser, but here, I can only see products of echo and print if they are simple strings I have entered, for example:
print_r("stack overflow is life");
This will print as expected in my browser. However, this is where me talking about 'live' script runs comes into play. Instead of reloading the page, it runs live. The messenger bot will always be active on the server, and it instantly replies to a message sent to it as wanted. I use this code:
/* receive and send messages */
$input = json_decode(file_get_contents('php://input'), true);
file_put_contents("fb.txt", file_get_contents('php://input'));
echo ("<pre>"); print_r($input);
echo ("</pre>");
Now, in this case, the $input is not printed. I see nothing. Now I don't know if this is to do with live server response, or what, but I need to know how to see this is the browser. And I have tested to see if there actually is a successfully converted JSON to array, because I am able to use the info in $input to reply to my facebook messaged and the bot works. I can also output the JSON to a txt file, and see it there, but there is no <pre> tags so it is hard to read, and I want the nice clean array to see in the browser. All code revolves around this, so it is very important.
So you are writing the raw input to the file and json decoding it separately. So it is quite possible you are not actually getting valid json.
If you do pass invalid json, json_decode returns NULL which is why you see that when you var_dump - so you have to call json_last_error to be sure it worked.
From Docs:
http://php.net/manual/en/function.json-decode.php
Returns the value encoded in json in appropriate PHP type. Values true, false and null are returned as TRUE, FALSE and NULL respectively. NULL is returned if the json cannot be decoded or if the encoded data is deeper than the recursion limit.
You should really check if json_decode works, here is an example to demonstrate:
<?php
$badjson = '{bad:"json"}';
$decoded = json_decode($badjson);
if(json_last_error()!==JSON_ERROR_NONE){
echo "Json Decode Failed: ".json_last_error_msg();
}else{
var_dump($decoded);
}
echo "\n---\n";
$goodjson = '{"property":"value"}';
$decoded = json_decode($goodjson);
if(json_last_error()!==JSON_ERROR_NONE){
echo "Json Decode Failed: ".json_last_error_msg();
}else{
var_dump($decoded);
}
See it in action here: http://sandbox.onlinephpfunctions.com/code/3a07e57f4cd01bd63d2945d5e367bbb0a6158195
See PHP Docs: http://php.net/manual/en/function.json-last-error.php
You can use a syntax checker to find the problem with your json e.g. http://jsonlint.com/
A common issue if the json is manually created is failing to wrap properties in double quotes e.g. {property:"value"} is invalid while {"property":"value"} is valid.
Note the reason you have to check json_last_error, and can't rely on NULL meaning it failed is because json_decode('NULL'); would return NULL and that would be correct.
Not sure what is cloud9.
For debug you can try var_dump() function. It will print onto your browser data type and data values, because there could be different type of "nothing". It's not a better way for debugging but a naive one. For better: check debug and breakpoint possibilities in this cloud9.
var_dump() can eat as many arguments as you like, so it's handy to dump everything with php input too to check what comes and how it changes.

Parse this data with PHP

Going to this webstie:
http://steamcommunity.com/market/priceoverview/?currency=3&appid=730&market_hash_name=AK-47%20%7C%20Redline%20%28Field-Tested%29
Yields this result:
{"success":true,"lowest_price":"5,59€ ","volume":"5,688","median_price":"5,92€ "}
The result is updated every time the page is refreshed. Using PHP, how would I be able to save the result line and split it up in my code I can use it for other things? Would it be viable/possible to do this about 3000-5000 times from a loop in my code, or would it be too much and crash it? I won't be using all the data from it in my code, just saving it into a database and moving to the next result.
That code is JSON and can be parsed with json_decode
$data = file_get_contents('http://steamcommunity.com/market/priceoverview/?currency=3&appid=730&market_hash_name=AK-47%20%7C%20Redline%20%28Field-Tested%29');
$json = json_decode($data);
echo $json->volume;
As far as looping... why? You would simply load the page 3000+ times in a row. What would be the benefit of that? Perhaps you should consider a cron job instead, which could fetch the data at regular intervals (and not spam the Steam servers)
Your code is in JSON format.
So:
$content = file_get_contents('http://steamcommunity.com/market/priceoverview/?currency=3&appid=730&market_hash_name=AK-47%20%7C%20Redline%20%28Field-Tested%29');
json_decode($content);
Should work correctly

PHP file_get_contents() and cURL both fail

I know this is probably old-hat for many of the good folks on this forum, but after a few hours of hunting here for a solution I'm still having trouble.
I have confirmed that I can get a simple call to file_get_contents() to work:
$content = file_get_contents('example.com');
print $content;
(Outputs the contents of the site, as expected.)
And, if I plug one of the URLs generated by my script into the browser directly, that works too:
http://patft.uspto.gov/netacgi/nph-Parser?Sect1=PTO2&Sect2=HITOFF&u=/netahtml/PTO/search-adv.htm&r=0&p=1&f=S&l=50&Query=AN/"allied signal" AND ((((((((CCL/29/$ OR CCL/62/$) OR CCL/165/$) OR CCL/180/$) OR CCL/236/$) OR CCL/237/$) OR CCL/241/$) OR CCL/248/$) OR CCL/417/$)&d=PTXT
(In actuality, that is generated/stored as a literal string named $url in my code. Though, I'm noticing as I type this that the quotes are throwing off something here. Not sure if that translates to my problem or not.)
But, when I try to combine the two it fails immediately:
$content = file_get_contents($url);
print $content;
(Outputs nothing.)
I've tried every suggestion I've seen so far involving various uses of cURL instead, and of course json to try to get some clue what is happening. But, other than the one case where I got HTTP 400 to spit out, and another where the output was NULL (sorry, I lost the links to the specific posts I'd been reading), no luck there.
I'm sure its something ludicrously simple that I'm missing here, that I'll probably ::head desk:: when I find it, but right now I'm stuck. Any suggestions?
Thanks In Advance
EDIT: I forgot to include, nothing in the error log either.
EDIT: allow_url_fopen is set correctly.
EDIT: Use of urlencode($url) does produce the following error, when the example above is called...
[29-May-2014 02:08:57 America/New_York] PHP Warning: file_get_contents(http%3A%2F%2Fpatft.uspto.gov%2Fnetacgi%2Fnph-Parser%3FSect1%3DPTO2%26Sect2%3DHITOFF%26u%3D%2Fnetahtml%2FPTO%2Fsearch-adv.htm%26r%3D0%26p%3D1%26f%3DS%26l%3D50%26Query%3DAN%2F%22allied+signal%22%0D%0A+AND+%28%28%28%28%28%28%28%28CCL%2F29%2F%24+OR+CCL%2F62%2F%24%29+OR+CCL%2F165%2F%24%29+OR+CCL%2F180%2F%24%29+OR+CCL%2F236%2F%24%29+OR+CCL%2F237%2F%24%29+OR+CCL%2F241%2F%24%29+OR+CCL%2F248%2F%24%29+OR+CCL%2F417%2F%24%29%26d%3DPTXT): failed to open stream: Invalid argument in C:\inetpub\wwwroot\PHP-CGI\test.php on line 21
EDIT: Some test code...
<?php
print "Testing...<BR>";
define("PATFT_head",'http://patft.uspto.gov/netacgi/nph-Parser?Sect1=PTO2&Sect2=HITOFF&u=%2Fnetahtml%2FPTO%2Fsearch-adv.htm&r=0&p=1&f=S&l=50&Query=AN%2F%22');
define("PATFT_foot",'%22+AND+%28%28%28%28%28%28%28%28CCL%2F29%2F%24+OR+CCL%2F62%2F%24%29+OR+CCL%2F165%2F%24%29+OR+CCL%2F180%2F%24%29+OR+CCL%2F236%2F%24%29+OR+CCL%2F237%2F%24%29+OR+CCL%2F241%2F%24%29+OR+CCL%2F248%2F%24%29+OR+CCL%2F417%2F%24%29&d=PTXT');
$assignees = file("./config/assignees.txt");
foreach ($assignees as $name) {
$url= PATFT_head.$name.PATFT_foot;
$content = file_get_contents($url);
#print "Should see the page here...<BR>";
print $content;
}
print "<BR>Done.";
?>
Contents of assignees.txt is a series of search terms (in this case the Assignee Name of a patent holder), 1 per line.
Try urlencode :
$content = file_get_content(urlencode($url));
print $content;
There is a setting for allow_url_fopen in the PHP INI. Make sure you set this to true or 1.

Single quotes in JSON from PHP causing problems

NOTE: this was a completely different question until I realized where the problem really was.
My current issue is that I am trying to output some JSON from PHP for use by jQuery. I am doing this cross-domain so I am using "JSONP". I have narrowed the problem down to the fact that there are single quotes in my JSON so when I output with the callback function I end up getting too many single quotes.
I have tried calling str_replace("'","\'",$value) in PHP and it seems to output as \\' in my JSON rather than \' which is apparently not readable by jQuery (though online JSON validators say the JSON is valid.
So what I need to know is how to get only a single slash in my string inside of PHP rather than 2 slashes.
What I'm gathering is this:
jQuery uses _ as a callback parameter to JSONP requests. It will automatically append ?_=jQuery<random_numbers> basically telling the server to call this unique function name when it returns. As such, as of recently (within the last year I want to say) .ajax takes care of making a callback function for you, then kind of "re-routes" it to a function you specify within the success property.
Also, your PHP code is using $_GET['callback'] when it now should be using $_GET['_'] (to stay more in-line with jQuery and what it's sending).
The "jQuery was not called" is just jQuery notifying you that anticipated callback wasn't included in the response, and it was looking to make sure it was getting called.
Short answer (as I see it) reference $_GET['_'] instead of $_GET['callback'] to satisfy jQuery.
try to use this in your PHP:
echo $_GET['callback'] . '(' . json_encode($your_array_data) . ')';
Regards.

Categories