Php call http url

https://

Allows read-only access to files/resources via HTTP. By default, a HTTP 1.0 GET is used. A Host: header is sent with the request to handle name-based virtual hosts. If you have configured a user_agent string using your php.ini file or the stream context, it will also be included in the request.

The stream allows access to the body of the resource; the headers are stored in the $http_response_header variable.

If it’s important to know the URL of the resource where your document came from (after all redirects have been processed), you’ll need to process the series of response headers returned by the stream.

The from directive will be used for the From: header if set and not overwritten by the Context options and parameters.

Usage

  • http://example.com
  • http://example.com/file.php?var1=val1&var2=val2
  • http://user:password@example.com
  • https://example.com
  • https://example.com/file.php?var1=val1&var2=val2
  • https://user:password@example.com

Options

Wrapper Summary

Attribute Supported
Restricted by allow_url_fopen Yes
Allows Reading Yes
Allows Writing No
Allows Appending No
Allows Simultaneous Reading and Writing N/A
Supports stat() No
Supports unlink() No
Supports rename() No
Supports mkdir() No
Supports rmdir() No

Examples

Example #1 Detecting which URL we ended up on after redirects

$meta_data = stream_get_meta_data ( $fp );
foreach ( $meta_data [ ‘wrapper_data’ ] as $response )

/* Were we redirected? */
if ( strtolower ( substr ( $response , 0 , 10 )) == ‘location: ‘ )

Читайте также:  Access class properties php

/* update $url with where we were redirected to */
$url = substr ( $response , 10 );
>

Notes

Note: HTTPS is only supported when the openssl extension is enabled.

HTTP connections are read-only; writing data or copying files to an HTTP resource is not supported.

Sending POST and PUT requests, for example, can be done with the help of HTTP Contexts.

See Also

User Contributed Notes 4 notes

Passing authentication information in the URL as in «https://user:password@example.com» works for HTTP «Basic» access authentication but not for HTTP «Digest» access authentication. You can use the cURL functions for servers requesting HTTP «Digest» access authentication.

«The stream allows access to the body of the resource; the headers are stored in the $http_response_header variable. Since PHP 4.3.0, the headers are available using stream_get_meta_data().»

This one sentence is the only documentation I have found on the mysterious $http_response_header variable, and I’m afraid it’s misleading. It implies that from 4.3.0 onward, stream_get_meta_data() ought to be used in favor of $http_response_header.

Don’t be fooled! stream_get_meta_data() requires a stream reference, which makes it ONLY useful with fopen() and related functions. However, $http_response_header can be used to get the headers from the much simpler file_get_contents() and related functions, which makes it still very useful in 5.x.

Also note that even when file_get_contents() and friends fail due to a 4xx or 5xx error and return false, the headers are still available in $http_response_header.

function post_it ( $datastream , $url )

$url = preg_replace ( «@^http://@i» , «» , $url );
$host = substr ( $url , 0 , strpos ( $url , «/» ));
$uri = strstr ( $url , «/» );

$reqbody = «» ;
foreach( $datastream as $key => $val ) if (!empty( $reqbody )) $reqbody .= «&» ;
$reqbody .= $key . » keyword»>. urlencode ( $val );
>

$contentlength = strlen ( $reqbody );
$reqheader = «POST $uri HTTP/1.1\r\n» .
«Host: $host \n» . «User-Agent: PostIt\r\n» .
«Content-Type: application/x-www-form-urlencoded\r\n» .
«Content-Length: $contentlength \r\n\r\n» .
» $reqbody \r\n» ;

$socket = fsockopen ( $host , 80 , $errno , $errstr );

if (! $socket ) $result [ «errno» ] = $errno ;
$result [ «errstr» ] = $errstr ;
return $result ;
>

while (! feof ( $socket )) $result [] = fgets ( $socket , 4096 );
>

Источник

URL call in PHP

Then you would run Solution 1: Yes you could supply that parameter into calling your user defined function: Might as well filter the ones you have defined thru Sample Output Sidenote: can be also applied as well: Solution 2: One option you can do if use if/elseifs like so: Or you could use a riskier approach and call the function name directly from the input. Edit: Or you could use a switch statement: Solution: If you want to call function from url like codeigniter do, i have an example for you URL example:

URL call in PHP

Does this do want you need?

$URL = "http://www.api.com/api.php?data=mydata"; $data = file_get_contents($URL); 

data now contains the response.

Did you try looking at cURL? http://php.net/manual/en/book.curl.php

$ch = curl_init();// init curl curl_setopt($ch, CURLOPT_URL,"http://www.api.com/api.php"); curl_setopt($ch, CURLOPT_POST, 1);// set post data to true curl_setopt($ch, CURLOPT_POSTFIELDS,"data=mydata&foo=bar");// post data // receive server response . curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);// gives you a response from the server $response = curl_exec ($ch);// response it ouputed in the response var curl_close ($ch);// close curl connection 

You need to POST data ? Check out http_post_data :

Web services — URL call in PHP, In fact I want to use PHP instread of typing that data in URL bar (that could be very nice haha) Thanks in advance ! It’s totally unclear what you are trying to achieve & what you already tried.. Try to add this to before the first php code line: $_GET [«data»] = «mydata»;. Please tell me if it works!

Current URL with PHP

$url = "http://" . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']; 

And I recommend to use urlencode( $url ) and urldecode( $url ) to wrap and unwrap it for transfering safely.

"http://www.yoursitesdomainname.com".$_SERVER['REQUEST_URI']; 

The previouse will be the current page’s URL, Do as you may with it.

As Babiker said that will return the URI. I would suggest filtering or exscaping the url. WordPress has a function called esc_url.

From WordPress core wp-includes/formatting.php lines 2235-2281

 /** * Checks and cleans a URL. * * A number of characters are removed from the URL. If the URL is for displaying * (the default behaviour) amperstands are also replaced. The 'clean_url' filter * is applied to the returned cleaned URL. * * @since 2.8.0 * @uses wp_kses_bad_protocol() To only permit protocols in the URL set * via $protocols or the common ones set in the function. * * @param string $url The URL to be cleaned. * @param array $protocols Optional. An array of acceptable protocols. * Defaults to 'http', 'https', 'ftp', 'ftps', 'mailto', 'news', 'irc', 'gopher', 'nntp', 'feed', 'telnet' if not set. * @param string $_context Private. Use esc_url_raw() for database usage. * @return string The cleaned $url after the 'clean_url' filter is applied. */ function esc_url( $url, $protocols = null, $_context = 'display' ) < $original_url = $url; if ( '' == $url ) return $url; $url = preg_replace('|[^a-z0-9-~+_.?#=!&;,/:%@$\|*\'()\\x80-\\xff]|i', '', $url); $strip = array('%0d', '%0a', '%0D', '%0A'); $url = _deep_replace($strip, $url); $url = str_replace(';//', '://', $url); /* If the URL doesn't appear to contain a scheme, we * presume it needs http:// appended (unless a relative * link starting with / or a php file). */ if ( strpos($url, ':') === false && substr( $url, 0, 1 ) != '/' && substr( $url, 0, 1 ) != '#' && !preg_match('/^[a-z0-9-]+?\.php/i', $url) ) $url = 'http://' . $url; // Replace ampersands and single quotes only when displaying. if ( 'display' == $_context ) < $url = preg_replace('/&([^#])(?![a-z];)/', '&$1', $url); $url = str_replace( "'", ''', $url ); > if ( !is_array($protocols) ) $protocols = array ('http', 'https', 'ftp', 'ftps', 'mailto', 'news', 'irc', 'gopher', 'nntp', 'feed', 'telnet', 'mms', 'rtsp', 'svn'); if ( wp_kses_bad_protocol( $url, $protocols ) != $url ) return ''; return apply_filters('clean_url', $url, $original_url, $_context); > 

PHP Get URL – How to Get the Full URL of the Current, With this variable, we will have to use 2 separate indices to get each part of the current page’s URL. The first part will be the host, localhost, and the second part will be the page name, home. The first index we will use is HTTP_HOST — The current web address host, for example localhost, or example.com. The …

How to call php function with URL?

Yes you could supply that parameter into calling your user defined function:

Might as well filter the ones you have defined thru get_defined_functions

function l1() < echo "Hello there!"; >function l2() < echo "I have no Idea what Im doing!"; >function l3() < echo "I'm just a year 1 college student dont torture me sir!"; >$functions = $arr = get_defined_functions()['user']; // defined functions if(!empty($_GET['function']) && in_array($_GET['function'], $functions))

Sidenote: function_exists can be also applied as well:

if(!empty($_GET['function']) && function_exists($_GET['function'])) < // invoke function >

One option you can do if use if/elseifs like so:

if($_GET['function'] == 'l1') < l1(); >else if($_GET['function'] == 'l2')

Or you could use a riskier approach and call the function name directly from the input.

$functionName = $_GET['function']; $functionName(); 

Edit: Or you could use a switch statement:

How to call an url in php file?, If I paste this url directly in my browser url barre this work well. What I would like is simply call this url through a php file such myscript.php. I’m not a php coder and I made some search about but did not find how to do (and especially how to search for such basic case). I tried for example this:

How to call a function in the URL (PHP)

If you want to call function from url like codeigniter do, i have an example for you

URL example: http://localhost/jastip/ajax/request.php/get_orders

(function () < $url_function = explode('/', $_SERVER['REQUEST_URI']); $function_name = get_defined_functions()['user']; if (in_array($url_function[4], $function_name)) < $index = array_search($url_function[4], $function_name); $dynamic_fun = $function_name[$index]; $dynamic_fun(); >else < var_dump("Page not found"); die; >>)(); function get_orders() < echo "get orders"; >function get_something()

How to call a function in the URL (PHP), I have another php script on another webpage to call from it. Currently I call the php script, using the php file name. I would like to check is there a way for me to call the php file using a function in the url? The reason is because, in the php script, I would have several functions to call from. I do not want to …

Источник

How to call URL of any other website in PHP

Here is how I might call the mean app. I am designing a platform that divides a monolith app into micro-services with high availability.

To the URL first set the PHP resource:

php // create a new cURL resource $ch = curl_init(); // set URL and other appropriate options curl_setopt($ch, CURLOPT_URL, "http://www.example.com/"); curl_setopt($ch, CURLOPT_HEADER, 0); // grab URL and pass it to the browser curl_exec($ch); // close cURL resource, and free up system resources curl_close($ch); ?>
header("Location:www.google.com");
$page = file_get_contents("http://www.domain.com/filename");

This does require FOpen which some web hosts disable and some web hosts will allow FOpen, but not allow access to external files. You may want to check where you are going to run the script to see if you have access to External FOpen. You can also use file_get_contents to access REST APIs:

$payload = file_get_contents('http://api.someservice.com/SomeMethod?param=value');

Or if you just want a simple URL GET then:

$lines = file('http://www.example.com/');

Источник

Оцените статью