file_get_contents() with no response - php - php

I am trying to call a web service which basicaly looks like this:
http://10.10.10.10:8080/gw/someAction?amount=10&description='Some description'
So this is how i call this web service:
$endpoint = "http://10.10.10.10:8080/gw/someAction?amount=10&description='Some description'";
$opts = array('http' =>
array(
'method' => 'GET',
'header' => 'Content-type: application/xml'
)
);
$context = stream_context_create( $opts );
$result = file_get_contents( $endpoint, false, $context );
$xml_result = simplexml_load_string( $result );
echo $xml_result->success;
So here, i got nothing, the xml_result is empty. And here is the interesting part - when i remove the blank space from the description:
http://10.10.10.10:8080/gw/someAction?amount=10&description='Somedescription'
Everything is just fine, I got the answer from the web service. Also tried to call the web service with the chrome rest client WITH the blank space in the description and everything is OK, i have response. So this leads me to some kind of PHP problem here with the blank spaces in the web service. Please, help !
UPDATE:
print_r($result)
results in
1

This is not a valid URL, spaces must be escaped:
http://10.10.10.10:8080/gw/someAction?amount=10&description='Some%20description'
You might want to take a look at How to properly URL encode a string in PHP?.

Related

Unable to get Healthline search results with PHP

I am trying to run a script that will search Healthline with a query string and determine if there are any search results, but I can't get the contents with the query string posting to the page. To search for something on their site, you go to https://www.healthline.com/search?q1=search+string.
Here is what I tried:
$healthline_url = 'https://www.healthline.com/search';
$search_string = 'ashwaganda';
$postdata = http_build_query(
array(
'q1' => $search_string
)
);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $postdata
)
);
$stream = stream_context_create($opts);
$theHtmlToParse = file_get_contents($healthline_url, false, $stream);
print_r($theHtmlToParse);
I also tried to just add the query string to the url and skip the stream, amongst other variations, but I'm running out of ideas. This also didn't work:
$healthline_url = 'https://www.healthline.com/search';
$search_string = 'ashwaganda';
$opts = array(
'http'=>array(
'method'=>"GET",
'header'=>"Content-Type: text/xml; charset=utf-8"
)
);
$stream = stream_context_create($opts);
$theHtmlToParse = file_get_contents($healthline_url.'&q1='.$search_string, false, $stream);
print_r($theHtmlToParse);
And suggestions?
EDIT: I changed the url in case someone wants to look at the search page. Also fixed the query string. Still doesn't work.
In response to Ken Lee, I did try the following cURL script that also just returns the page without search results:
$healthline_url = 'https://www.healthline.com/search?q1=ashwaganda';
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $healthline_url);
$data = curl_exec($ch);
curl_close($ch);
print_r($data);
Healthline does not load the search result directly. It has its search index stored in Algolia and made extra javascript calls to retrieve the result. Therefore you cannot see the search result by file_get_content.
To see the search result, you need to run a browser simulator that simulates a javascript-capable browser to properly run the site page.
For PHP developers, you may try using php-webdriver to control browers through webdriver (e.g. Selenium, Chrome + chromedriver, Firefox + geckodriver).
Update: Didn't know that the target site is Healthline. Updated the answer once I found out.

using a context-stream resource with file_get_contents returns a NULL string

I'm using PHP 4.3.9 and am trying to POST to a url without a form using stream_context_create like below:
function do_post_request($url, $postdata) {
$content = "";
foreach($postdata as $key => $value)
$content .= "$key=$value&";
$content = urlencode($content);
$params = array('http' => array(
'method' => 'POST',
'header' => 'Content-Type: application/x-www-form-urlencoded',
'content' => $content
));
$ctx = stream_context_create($params);
$result = file_get_contents($url, false, $ctx);
var_dump($result);
This code is taken almost word for word from the php manual and I've seen it in several places here on stackoverflow as well.
If I do file_get_contents without $ctx, var_dump($results) will display the $url properly (but without the necessary changes $_POST would cause, of course). With $ctx, var_dump($result) is NULL. So something is wrong with $ctx but I have no idea what. Am I setting up my $params incorrectly or something?
Any insight would be appreciated. If there is another way to pass POST data to a url I wouldn't mind hearing that either. But I cannot use cURL (or anything that needs installation) and I'm using an older version of php so my choices are limited.
Thanks

Formatting JSON Data using PHP

I have the following code:
<?php
$ids = 'ids=com.hugogames.hugotrollwars';
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $ids
)
);
$context = stream_context_create($opts);
$result = file_get_contents('https://play.google.com/store/xhr/getdoc', false,
$context);
$convert = explode("\n", $result);
Basically its pulling permissions from the Playstore and displaying it as a string. The issue I am having is removing the un needed data (image links, description) and only show the permissions and the permissions description. I tried using the json_decode function with php and it returned NULL.
Is there something i'm missing?
Any help is greatly appreciated!
$result is not valid json. Google Play API is using a protobuf variant.
http://www.segmentationfault.fr/publications/reversing-google-play-and-micro-protobuf-applications/
There are also php libraries to talk to google play.
https://github.com/splitfeed/android-market-api-php
https://github.com/thetutlage/Google-Play-Store-API

Can't send data in post request via file_get_contents

Need to request some code two times in my app. Fist request url as ajax call, and also need to request this url in controller (something like hmvc). I know how to develop this via curl but I found another kind of idea how to implement this, just use function file_get_contents with before prepared params. This my code:
// Setup limit per page
$args['offset'] = $offset;
$args['limit'] = $this->_perpage;
// --
// Convert search arguments to the uri format
$data = http_build_query($args);
// Define request params
$options = array(
'http' => array(
'header' => 'Content-type: application/json' . PHP_EOL .
'Content-Length: ' . strlen($data) . PHP_EOL,
'method' => 'POST',
'content' => $data,
),
);
$context = stream_context_create($options);
$result = file_get_contents(
'http://'.$_SERVER['HTTP_HOST'].'/search/items', FALSE, $context
);
Request method was detected ok in requested uri, but params wasn't passed. Why this is not pass arguments to request? Where is bug in my code? Many thanks for any answers.
http_build_query builds application/x-www-form-urlencoded content. (not application/json)
There is a full example:
How to post data in PHP using file_get_contents?
Content type should be application/x-www-form-urlencoded. If you want to stay with application/json, try to get posted data using file_get_contents("php://input").

Retrieving a PHP file using file_get_content(), cURL() or fopen() all return PHP code itself

I'm retrieving a PHP file using file_get_content(), cURL() or fopen() all return PHP code itself. It's not retrieving just the HTML code itself that PHP is outputting. How can I overcome this please?
It's basically generating an HTML newsletter preview for my customer that is generated from their CMS. However I want them to be able to copy and paste the HTML it generates into Mailchimp or such like. It just fetches the entire PHP source code though as well as the HTML :(
Hope you can help.
Thanks in anticipation.
Pete
Trying to read a local PHP file will return its source code, since its a "local" file, and is not parsed by the server.
If you wish to read the result of the PHP file you must request it from the server.
Meaning instead of doing:
$data = file_get_contents('x.php');
You should be doing
$data = file_get_contents('http://mysite.com/path/x.php');
Which will be parsed in the server, and then returned to you.
Cheers! :)
Shai.
This will work in localhost. Try this one: $url = "http://localhost/box.php";
function isAllBoxContents($boxid,$pageNum)
{
$postdata = http_build_query( array('myBoxid' => "$boxid", 'isAllBoxContents' => "all",'pageNum' => "$pageNum",'innerReq' => true ));
$opts = array('http' => array( 'method' => 'POST', 'header' => 'Content-type: application/x-www-form-urlencoded', 'content' => $postdata ));
$context = stream_context_create($opts);
$url = "http://localhost/box.php";
$result = file_get_contents( $url, false, $context);
$cacheFileName = "isAllBoxContents".$boxid.$pageNum;
cacheMe($cacheFileName,$result);
}

Categories