how to stop & from changing to &amp in a url? - php

I have a url like this, https://www.example.com/send?id=1&text=http://example2.com/song.mp3
when I try to get the data of the url using file_get_contents() it shows below error,
PHP Warning: file_get_contents(https://www.example.com/send?id=1&amptext=http://example2.com/song.mp3):
failed to open stream: HTTP request failed! HTTP/1.1 501 Not Implemented
and the & have converted to &amp. due to that the url is not working.
I have tried htmlspecialchars_decode() and preg_replace() on & , but it didn't do anything.
how to solve this ?

I think you better to use cURL instead of file_get_contents(), because referring server must be enabled allow_url_fopen in their server if you are using file_get_contents(), but cURL is a library it just make a http requests
<?php
$cSession = curl_init();
curl_setopt($cSession,CURLOPT_URL,"https://www.example.com/send?id=1&text=http://example2.com/song.mp3");
curl_setopt($cSession,CURLOPT_RETURNTRANSFER,true);
curl_setopt($cSession,CURLOPT_HEADER, false);
$result=curl_exec($cSession);
curl_close($cSession);
echo $result;
?>
refer following links
file_get_contents script works with some websites but not others
cURL vs file_get_contents

take a look at this sir, where $str="your link"
sample
$str = "https://www.example.com/send?id=1&text=http://example2.com/song.mp3";
echo html_entity_decode($str);
will output the one you want....

Related

Just call a https URL with PHP (without waiting for response)

I already googled and searched for an answer, and found also one, but I still get an error...
I just want to start a cronjob by calling a certain URL but I get this failure message:
PHP Warning: fopen(https://my-url.de): failed to open stream: HTTP request failed! in /var/www/vhosts/httpdocs/cronjob/import.php on line 41
I tried this one:
ignore_user_abort(true);
set_time_limit(0);
But it is not working... The url triggers a cronjob but this one needs to long to response...
So what would you suggest to call a cronjob in PHP?
I must call the cronjob by URL and I do not need to get a response!
Greetings and Thank You!
you can use curl instead of fopen as follows
$url = 'https://my-url.de';
$ch = curl_init($url);
// this will prevent printing out the contents , unless you are make echo $content
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$content = curl_exec($ch);
curl_close($ch);

Simple html dom not working for a table[class]

I want to use the contents of a website and include them in mine with "Simple Html DOM", unfortunately the code which normally works for another websites, fails in this specific case:
<?php
// Note you must download the php files from the link above
// and link to them on this line below.
include_once('simple_html_dom.php');
$html = file_get_html('http://www.comelmar.it/index.aspx?idmnu=23&midx=3&mbidx=2&idmnub=8&Lang=EN');
$elem = $html->find('table[class=Descrizione]', 0);
echo $elem;
?>
Warning:
file_get_contents(http://www.comelmar.it/index.aspx?idmnu=23&midx=3&mbidx=2&idmnub=8&Lang=EN)
[function.file-get-contents]: failed to open stream: HTTP request
failed! HTTP/1.1 500 Internal Server Error in
public_html/elin-custom-code/simple_html_dom.php on
line 75
Fatal error: Call to a member function find() on a non-object in
public_html/elin-custom-code/sklad-1.php on line 9
As a link for the reference:-
file_get_contents - failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found
Most hosting provides now block the furl_open parameter which allows you to use file_get_contents() to load data from an external url.
You can use CURL or a PHP client library like Guzzle.
To be make sure i just use CURL to check what is the exact problem with the help of below code:-
<?php
$curl_handle=curl_init();
curl_setopt($curl_handle, CURLOPT_URL,'http://www.comelmar.it/index.aspx?idmnu=23&midx=3&mbidx=2&idmnub=8&Lang=EN');
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handle, CURLOPT_USERAGENT, 'Your application name');
$query = curl_exec($curl_handle);
curl_close($curl_handle);
echo "<pre/>";print_r($query);
?>
Output:- http://prntscr.com/a91nl5
So it seems that for security reason these method calls(CURL and file_get_contents) are blocked on that site.
You can try this link answers also, may be you will get some useful output:-
PHP file_get_contents() returns "failed to open stream: HTTP request failed!"
Good Luck.

file_get_contents failed to open stream with a valid url

I'm trying to use this code in a php file.
I want get the source code from this url and parse the content.
<?php
$fuente = file_get_contents('http://www.akiracomics.com');
echo $fuente;
?>
The problem is, after execute the code I received this error
Warning: file_get_contents(http://www.akiracomics.com) [function.file-get-contents]: failed to open stream: Connection timed out in XXXXXX/test.php on line 2
I tried from the same server to other url and works perfect.
Any idea?
Thanx
It's working here, probably you're making too many requests (what are you trying to do, by the way?) and they are blocking incoming requests from your server.
I finally found the solution. I need to use a curl_exec instead file_get_contents.
With this code, everything works fine.
$curlIMG = curl_init();
curl_setopt($curlIMG, CURLOPT_URL, "http://www.google.es");
curl_setopt($curlIMG, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curlIMG, CURLOPT_HEADER, false);
$imgBinary = curl_exec($curlIMG);
curl_close($curlIMG);

PHP CURL GET request returns 500 where bash curl succeeds

I'm attempting to use PHP's CURL to make a GET request to a server and am having some difficulties doing so. When I make the request through PHP I am returned a 500 error from the external server. However, if I make the request using the bash curl, or visit the URL in a browser it succeeds.
I've stripped the PHP down to the bare essentials:
$url = 'http://example.com:8080/path/to/service?cmd=my_command&arg=example2.com';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_exec($ch);
print_r(curl_getinfo($ch));
curl_close($ch);
As stated this returns a 500 error from example.com. However, if I do the following:
[me#host ~] curl "http://example.com:8080/path/to/service?cmd=my_command&arg=example2.com"
I am returned the expected XML document.
What gives? It's got to be something with the encoding of the URL, as if I strip the $url var down to just http://example.com:8080 the PHP CURL request now responds with 200. I've tried replacing the & with %26 - that didn't work (nor would I expect it to, as & is valid in the URL there). I've tried doing what the answer for php curl sending vars using GET wierd results suggested, but that didn't help either.
What am I missing here? I'm sure that it's something absurdly simple, but it's escaping me.
Thanks!
EDIT: I've just attempted doing this in Python - just to see what happened - and it works fine there:
import urllib2
r = urllib2.urlopen(theURL)
r.read()
It turns out that the API I was accessing required a User-Agent for all requests, but did not provide any information to indicate such.
Is this a common thing? I can't find any other examples of anyone else doing this other than http://developer.github.com/v3/#user-agent-required
I was able to get things working just fine by adding
curl_setopt($ch, CURLOPT_USERAGENT, "User-Agent: Some-Agent/1.0");

PHP QueryString 404 Object Not Found

I am an Android developer having very little knowledge of PHP. I am stuck with this problem of getting 404 Error Object Not Found when reading variables from query string. I added .htaccess file as a result the error does go away but the variables are not read. Although if I read a URL without a query string it works perfectly. I can not think of a reason it should not work. Here's what I have tried:
PHP file:
Name of file: file_getTest.php
function file_get_contents_curl($url) {
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$baseUrl='http://xyz.com?language=en';
$language = $_GET['language'];
$session = $_GET['sessionID'];
$placeOrigin=$_GET['place_origin'];
$typeOrigin=$_GET['type_origin'];
$nameOrigin=$_GET['name_origin'];
$homePage =($baseUrl."&sessionID=".$session."&place_origin=".$placeOrigin."&type_origin=".$typeOrigin."&name_origin=".$nameOrigin);
//if "echo file_get_contents($baseUrl)" is executed it works fine
//echo file_get_contents($homePage); //I have tried file_get_contents but no use
echo file_get_contents_curl($homePage);
.htaccess code:
RewriteEngine on
RewriteRule .* file_getTest.php
Without .htacess file & using curl() I get this error:
The requested URL /test/file_getTest.php&sessionID=value read&place_origin=value read&type_origin=value read&name_origin=value read was not found on this server.
With .htaccess file & using curl() I get the following error:
HTTP/1.1 404 Object Not Found
Without .htacess file & using file_get_contents() I get this error:
The requested URL /test/file_getTest.php&sessionID=value read&place_origin=value read&type_origin=value read&name_origin=value read was not found on this server.
With .htaccess file & using file_get_contents() I get the following error:
file_get_contents(http://xyz.com?language=en&sessionID=&place_origin=&type_origin=&name_origin=): failed to open stream: HTTP request failed! HTTP/1.1 404 Object Not Found in C:\wamp\www\file_getTest.php on line 20
I would greatly appreciate any help. I have been stuck on this issue for some time now & can't seem to find a solution.
The problem was resolved. This seems pretty weird but all I did was to swap double quotes "" in $homepage with single quotes in the rest of the variables. After that, I encoded all the query string parameters & it worked like a charm.
so final code looks like this:
$baseUrl="http://xyz.com?language=en";
$session = $_GET["sessionID"];
$placeOrigin=$_GET["place_origin"];
$typeOrigin=$_GET["type_origin"];
$nameOrigin=$_GET["name_origin"];
$placeDestination=$_GET["place_destination"];
$typeDestination=$_GET["type_destination"];
$nameDestination=$_GET["name_destination"];
$sessionE=urlencode($session);
$placeOriginE=urlencode($placeOrigin);
$typeOriginE=urlencode($typeOrigin);
$nameOriginE=urlencode($nameOrigin);
$placeDestinationE=urlencode($placeDestination);
$typeDestinationE=urlencode($typeDestination);
$nameDestinationE=urlencode($nameDestination);
$homepage=$baseUrl.'&sessionID='.$sessionE.'&place_origin='.$placeOriginE.'&type_origin='.$typeOriginE.'&name_origin='.$nameOriginE.'&place_destination='.$placeDestinationE.'&type_destination='.$typeDestinationE.'&name_destination='.$nameDestinationE;

Categories