I've a probably really silly question about file_get_contents, I want to retrieve content on a website and this works properly when I serve to the below code:
$file = file_get_contents( $dataSourceURL, false, $context );
$dataSourceURL = 'http://somedomain.com', but when I'm serving a specific page of this website like $dataSourceURL = 'http://somedomain.com/c-4-hookah-coals-hookah-charcoal.html' it returns nothing.
Does anybody know something about this issue?
UPDATE
I know we can pass GET params using the below code:
$getdata = http_build_query(
array(
'var1' => 'some content',
'var2' => 'doh'
)
);
$opts = array('http' =>
array(
'method' => 'GET',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $getdata
)
);
$context = stream_context_create($opts);
but than... is there a simple way to resolve this kind of url query 'c-4-hookah-coals-hookah-charcoal.html' ?
file_get_contents works like unix wget, so you can try simply:
echo file_get_contents('http://www.hookah-shisha.com/c-4-hookah-coals-hookah-charcoal.html');
Related
I'm trying to send a special character, such as ñ, through a POST request in PHP. When I do it, it comes out as ñ, what is wrong and how do I fix it?
I'm sending and receiving the post request in PHP, here is what I use to send it:
$url = '<url>';
$data = array('key' => 'ñ');
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data)
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
echo $result;
Thanks in advance!
Try using this:
$data = array('key' => urlencode('ñ'));
And in the $url file:
$_POST['key']=urldecode($_POST['key']);
This is how I use to send special characters in GET and POST method with ajax, it must work for php too.
I need to submit a form to a website (http://www.itu.int/online/mms/mars/ship_search.sh) and I use the following code:
$postdata = http_build_query(
array(
'sh_mmsi' => '246709000'
)
);
$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('http://www.itu.int/online/mms/mars/ship_search.sh', false, $context);
For some reason is not triggering the search query. If I hit the Submit button in the URL address http://www.itu.int/online/mms/mars/ship_search.sh?sh_mmsi=246709000, then the results shows at the end. I wonder if the problem is that this website has two forms and I would need to specify which one to post to? If so, how should I do it?
I've been looking for a solution for such a long time for my issue.
But i just can't find any solutions and i'm not sure what exactly to search for.
I'll explain my problem here.
Currently i have this PHP script which works perfect and sends a successfully request and receives the respond.
<?php
$url = 'http://example.nl/index.php';
$data = array('url' => 'http://www.myip.nl/', 'verkort' => 'Verkort URL');
// use key 'http' even if you send the request to https://...
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data),
),
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
var_dump($result);
?>
But i really need help with sending a request with the following content:
See screenshot: http://i.imgur.com/VgedJes.png
I need to add the 3 headers and i'm not sure how to tell a field/value is empty as you can see in the screenshot.
I just can't figure out how to do this.
I would really appreciate it if someone could help me out with this!
To Add Header :
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\nX-Requested-With: XmlHttpRequest",
'method' => 'POST',
'content' => http_build_query($data),
),
);
Keep in mind that X-Requested-Header can only be interpreted by your PHP application
To send a parameter with an empty value you can do as follow
$data = array('url' => 'http://www.myip.nl/', 'verkort' => 'Verkort URL', 'notMandatoryParam' => '');
Also in your PHP code, you must do a verification : if(empty($_POST['notMandatoryParam'])) {/*SKIP ME*/}
I have no clue why... But all the methods I listed in the title are slow. They take about 10 seconds, yet when I visit the sites, they load immediately. Here is one of my codes that uses file_get_conents:
<?php
$search = $_GET['search'];
$postdata = http_build_query(
array(
'searchnode' => '' . $search . ''
)
);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Connection: close',
'content' => $postdata
)
);
$context = stream_context_create($opts);
$result = file_get_contents('http://google.com', false, $context);
echo $result;
?>
This code is just an example of what I use. But even using this code, it loads slow. I am using "Connection: close" also. Is this possibly a problem with the PHP config, or something else? I am using CentOS with cPanel + WHM. I do have shell and root access.
It could be that the DNS on your server is slow, try this:
replace
$result = file_get_contents('http://google.com', false, $context);
with
$ip = gethostbyname('google.com');
$result = file_get_contents("http://$ip", false, $context);
Hi I'm trying to send post values using a PHP script to an external website and get the result. My code is as follows
$postdata = http_build_query(
array(
'drpservice' => 091,
'drpdirection' => 1,
'drpbusstop' => 18051
)
);
$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);
echo $result;
However, it won't return the required results unless I can manage to send in the hidden _VIEWSTATE value.
Anyone able to help?
You need to first get the form page using a GET request, and then you can parse the page to get the _VIEWSTATE value using a regular expression.
Then when doing the POST pass the value you got for it.