After a long research on internet , I have found a method that seems to work , but not on the website I want.
<?php
$username = '';
$password = '';
$url = 'https://web.esme.fr/pscripts/bsp';
$context = stream_context_create(array(
'http' => array(
'header' => "Authorization: Basic " . base64_encode("$username:$password")
)
));
$data = file_get_contents($url , false, $context);
echo($data);
?>
I have tried this on another part of the website and it works , but on the page I want $data contains nothing !!!!
I know that sometimes this url asks 2 times the authentication , sometimes not.
Any suggestion ?
Thank you
Related
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.
This is my code:
if ($_SERVER ["REQUEST_METHOD"] === "GET") {
include_once('../database/dbSource.php');
$databaseSource = DataBaseSource::getInstance();
//Parse
$ini_array = parse_ini_file("../../pto/config.ini");
$user = $_GET['userSid'];
$username = $ini_array['ctsi_CIBMON_fid'];
$password = $ini_array['ctsi_CIBMON_pass'];
$ctsi_url = $ini_array['ctsi_url'] . $user . '&view=full';
$opts = array(
'http' => array(
'method' => "GET",
'header' => "Content-Type: application/json\r\n" . "Accept: application/json\r\n" .
"Authorization: Basic " . base64_encode("$username:$password")
)
);
$context = stream_context_create($opts);
echo file_get_contents($ctsi_url, false, $context);
} else {
echo $_SERVER ["REQUEST_METHOD"];
}
I get the following error :
There was an error parsing the JSON document. The document may not be
well-formed.
I tried checking addiitonal spaces in code (which i removed) etc.
Also there is no issues with authorization.
Can't figure out what's the issue.
Please help.
Note: Also it used to work but stopped working without any change to code even. so Strange.
also the url that i am trying to access is https://xxx.xxx.net not http - does it matter?
I have the API request as outlined below which works fine (given the correct replacement of "xxxyyy"!) however I want to perform this call simultaneously with up to 5-10 different remote URLs. In the example below I've shown just 1 remote URL however I have an array of 10,000 urls which I would like to query as quickly as possible, all of which return the same structure in JSON.
After researching the topic I believe this can be done in PHP using Curl Multi, does anyone know if this is true, if so how would I go about this so i can call say 10 at once rather than each one individually?
<?php
$username = "xxxyyyxxxyyyxxxyyyxxxyyy";
$password = "xxxyyyxxxyyyxxxyyyxxxyyyxxxyyyxxxyyyxxxyyyxxxyyy";
$remote_url_1 = 'https://xxxyyyxxxyyyxxxyyyxxxyyy_1.json';
{
$headers = array();
$headers[] = "Authorization: Basic " . base64_encode("$username:$password");
$headers[] = "X-Page:" . $pages;
$opts = array(
'http'=>array(
'method'=>"GET",
'header' => $headers
)
);
$context = stream_context_create($opts);
$file1 = file_get_contents($remote_url, false, $context);
$data = json_decode($file1, true);
$data2 = (array_values($data));
$orderline_id = $data2[0];
$orderline_sale_number = $data2[1];
$orderline_status = $data2[2];
$orderline_notes = $data2[3];
}
}
?>
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
so I've been working on a script for a while now and can't figure out one thing. For the site putlocker.com I want to generate a unique url for videos. So essentially it takes a standared url and from that I generate a direct url. I already post data and generate another link, in the new page there is another link that is the actual video link. However, this URL changes with every IP Address. The only way I figured it would work is to load the $search2url in an iframe and get the source of the iframe which I can then parse. However, I realized that this is not possible because it is not on my domain. If there is a way I can do that or any other way I can load the $search2url and parse the unique link please let me know. Thanks!
If you run the script on a server and try to play the video, the video won't play. However, if you go to the link from the start the video plays.
<?php
$url = "http://www.putlocker.com/file/2CE47CB3D2292208";
$contents = file_get_contents($url);
$find = strpos($contents, '<input type="hidden" value="');
$part1 = substr($contents, $find+28);
$hash = substr($part1, 0, strpos($part1, '"'));
$postdata = http_build_query(
array(
'hash' => $hash,
'confirm' => 'submitButton'
)
);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $postdata
)
);
$context = stream_context_create($opts);
$result = file_get_contents($url, false, $context);
$url1 = substr($result, strpos($result, "/get_file.php?"));
$url2 = substr($url1, 0, strpos($url1, "'"));
$search2url = "http://www.putlocker.com$url2";
$res2 = file_get_contents($search2url);
$firstfinal1 = substr($res2, strpos($res2, 'url="')+5);
$finalmediaurl = substr($firstfinal1, 0, strpos($firstfinal1, '"'));
//echo $finalmediaurl;
echo $result;
?>