I have never delt with proxies but now I have to. The program is a PHP parser that scraps several websites to retrieve need info. I just need to know how PHP programs work via proxies... how to make them work via proxies.
Thanks for any help!
You can use curl with CURLOPT_PROXY Option.
http://www.php.net/manual/en/function.curl-setopt.php
http://php.net/stream_context_create
For example:
<?php
$opts = array ( 'http' => array ( 'proxy' => 'tcp://proxy:8080', 'request_fulluri' => true ) ) ;
$context = stream_context_create ( $opts ) ;
$f = file_get_contents ( 'http://yoururl/', false, $context ) ;
Related
I need to check for the HTTP Response from a XML endpoint in PHP
http_response_code() returns 200ok, but I need to know the response from the actual endpoint.
I think I am blocked on the firewall because of prior failures.
Please be kind and ask me if I am not making myself clear. I understand it should be fairly straightforward.
$auth = base64_encode ( $this->config->item ( "user" ) . ":" . $this->config->item ( "pass" ) );
$context = stream_context_create ( array ('http' => array ('header' => "Authorization: Basic $auth" ) ) );
$unitsXML = file_get_contents ( $this->config->item ( "server_url" ) . "/", false, $context );
libxml_use_internal_errors ( true );
$objXML = simplexml_load_string ( $unitsXML );
var_dump(http_response_code()); // <- this returns 200ok, ut how to get this code but from simplexml http request?
At the moment I only have a blank page with null
I'm trying a new service to do a specific task, they have a pretty basic API which lets the users build simple apps.
An example of the HTTP request is:
https://domainname.com/dashboard/api
?to={PHONE NUMBER}&from={SENDER ID}&message={TEXT}
&email={YOUR EMAIL}&api_secret={API SECRET}
&unicode={TRUE/FALSE}&id={IDENTIFIER}
I have tried everything, using postman,php and googling for the past 3 hours and i can't get it to work.
(even tried to send it through the browser lol)
Whats a proper way to send a http request?
Thank you.
You can send an HTTP request without CURL using PHP5. This answer is based off of How do I send a POST request with PHP?. You'll have to enter your variables into the $data array.
$url = 'https://domainname.com/dashboard/api';
$data = array('to' => PHONE_NUMBER, 'from' => SENDER_ID, 'message' => TEXT, 'email' => EMAIL, 'api_secret' => SECRET, 'unicode' => BOOLEAN, 'id' => IDENTIFIER);
$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);
if ($result === FALSE) { /* Handle error */ }
var_dump($result);
You can test if your requests are working from your script by using http://www.postb.in or some other similar service. This allows you to see your requests to debug them (rather than relying on feedback/errors from the service you are using). Then you'll know if your requests are formatted correctly and working. Sometimes a service is down... and you spend hours troubleshooting something that's not on your end.
So i need to gain access to a web service containing some json, but to do so I was told to make use of PHP POST method to first log into the web service. I was giving an array with 3 types/values.
{
"Username":"user",
"password":"1234",
"LoginClient":"user"
}
I have been searching all day for a solution, but have come up short :(.
Any advice or push into a right direction would be much appreciated.
Hope I have explained this clearly enough.
you could do as follows:
$url = 'http://yourDomain.net/api/auth/';
$data = array('Username' => 'user', 'password' => '1234', 'LoginClient' => 'user');
$opts = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data),
)
);
$context = stream_context_create($opts); //Creates and returns a stream context with any options supplied in options preset.
$response = file_get_contents($url, false, $context);
var_dump($response);
Or you could read about CURL as another option to make POST requests.
Is there way to have a php script that uses file_get_contents to utilize an ip address different than what the server's ip address is? We have 5 ip addresses and want to utilize a specific one for this purpose.
Yes, it's possible. You have to configure the stream context you want to use though.
<?php
// context options
$ctxopts = array(
'socket' => array(
'bindto' => '192.168.0.100:0',
),
);
// create the context...
$context = stream_context_create($ctxopts);
// ...and use it to fetch the data
echo file_get_contents('http://www.example.com', false, $context);
You can get more info on http://php.net/manual/en/context.socket.php
Per the documentation that is easily accessible on php.net
context
A valid context resource created with stream_context_create(). If you don't need to use a custom context, you can skip this parameter by NULL.
stream_context_create()'s documentation explains the rest
$opts = array(
'socket' => array(
'bindto' => '123.123.123.123:0', // 0 for automatically determine port
)
);
final code:
$opts = array(
'socket' => array(
'bindto' => '123.123.123.123:0', // 0 for automatically determine port
)
);
$stream = stream_context_create($ctxopts);
echo file_get_contents('http://www.website', false, $stream);
I want to send a GET request to an external site, but also want to send some parameters
for example i've to send a get request to example.com
i want to execute www.example.com/send.php?uid=1&pwd=2&msg=3&phone=3&provider=xyz
My code is :
$getdata = http_build_query(
array(
'uid' => '1',
'pwd' => '2',
'msg'=>'3',
'phone'=>'9999',
'provider'=>'xyz'
)
);
$opts = array('http' =>
array(
'method' => 'GET',
'content' => $getdata
)
);
$context = stream_context_create($opts);
$result = file_get_contents('http://example.com/send.php', false, $context);
I get a server error .
The content option is used with POST and PUT requests. For GET you can just append it as a query string:
file_get_contents('http://example.com/send.php?'.$getdata, false, $context);
Furthermore, the method defaults to GET so you don't even need to set options, nor create a stream context. So, for this particular situation, you could simply call file_get_contents with the first parameter if you wish.