PHP cURL doesn't work with - in subdomains - php

I have a PHP script that I'm trying to get the contents of a page. The code im using is below
$url = "http://test.tumblr.com";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$txt = curl_exec($ch);
curl_close($ch);
echo "$txt";
It works fine for me as it is now. The problem I'm having is, if I change the string URL to
$url = "http://-test.tumblr.com"; or $url = "http://test-.tumblr.com";
It will not work. I understand that -test.example.com or test-.example.com is not a valid hostnames but with Tumblr they do exists. Is there a work around for this?
I even tried creating a header redirect on another php file so cURL would be first getting a valid hostname but works the same way.
Thank you

Domain Names with hyphens
As you can see in a previous question about the allowed characters in a subdomain, - is not a valid character to start or end a subdomain with. So this is actually correct behavior.
The same problem was reported over the curl mailing list some time ago but since curl follows the standard, there is actually nothing to change on their site.
Most likely tumblr knows about this and therefore offers some alternative address leading to the same site.
Possible workaround
However you could try using nslookup to manually lookup the IP and then send your request directly to this IP (and manually setting the hostname to the correct value). I didn't try this out, but it seems as if nslookup is capable to resolve malformatted domain names that start or end in a hyphen.
curl
Additionally you should know, that the php curl function should be a direct interface to the curl command line tool and therefore, if you would encounter special behavior it would most likely be due to the logic in the curl command line tool and not the php function.

Related

add a php variable onto an url for cURL use

I have a search box which is passing the user input into my cURL, to make the search on the webservice. I thought of adding the inputs (or variables) to the URL I'm using to connect to via cURL.
However I think I'm doing it wrong.
The hostname was removed on purpose for the post.
// Specify the URL to connect to - this can be PHP, HTML or anything else!
curl_setopt($connection, CURLOPT_URL, "https://WebService.php?aRegion='$aRc'&aType=empty");
I am suppossed to had something more besides the '..' when using a php variable into a link right?
That URL you currently use does not really make sense...
issue: you forgot to specify a host name. Without such the http client tries to resolve hWebService.php via the domain name resolution which will fail since that is not a valid host name.
you are using invalid characters in your URL.
Here is an approach that should be close to what you finally need, is easy to read (that is important!) and also takes care of url encoding the variable values:
curl_setopt(
$connection,
CURLOPT_URL,
sprintf(
"https://example.com/hWebService.php?aRegion=%s&aType=empty",
urlencode($aRc)));
Note: "example.com" obviously is only an example.
Update: you ask how to inject the value of a second variable as aType get parameter. Here the modified version:
curl_setopt(
$connection,
CURLOPT_URL,
sprintf(
"https://example.com/hWebService.php?aRegion=%s&aType=%s",
urlencode($aRc),
urlencode($aType)));
Not exactly sure what you're getting at or the process you're taking to get the information but you could try this.
$user_input1 = $_GET['information_you_need_to_append_to_curl'];
$user_input2 = $_GET['other_user_info'];
$web_address = sprint_f("https://WebService.php?aRegion=%s&aType=%s", $user_input1, $user_input2);
curl_setopt($connection, CURLOPT_URL, $web_address);
This will retrieve information from a get request made to your PHP document and then add it to your web address at the end of your curl request.
Check out the php documentation for more information on the sprint_f function

Bittrex API With PHP

I'm trying to set up a bot for bittrex by using the bittrex api. I previously tried using python but had a hard time as the documentation was in php(https://bittrex.com/Home/Api), so I decided to switch to php. Im trying to create the bot but having a hard time starting. I pasted the initial code:
$apikey='xxx';
$apisecret='xxx';
$nonce=time();
$uri='https://bittrex.com/api/v1.1/market/getopenorders?
apikey='.$apikey.'&nonce='.$nonce;
$sign=hash_hmac('sha512',$uri,$apisecret);
$ch = curl_init($uri);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('apisign:'.$sign));
$execResult = curl_exec($ch);
$obj = json_decode($execResult);
And according to this video: (sorry I had to add space because it doesn't allow me to post more than 2 links with low rep)
https:// youtu.be/K0lDTK3D-74?t=5m30s
It should return this: (Same as Above)
http:// i.imgur.com/jCoAUT9.png
But when I try place the same thing in a php values, with my own api key and secret I just get a blank webpage with nothing on it. This is what my php file looks like(API key and secret removed for security reasons):
http://i.imgur.com/DYYoY0g.png
Any idea why this could be happening and how I could fix it?
Edit: No need for help anymore. I decided to go back to python and try to do it there and finally made it work :D
The video you're working from has faked their results. Their code doesn't do anything with the value of $obj, so I wouldn't expect anything to show up on the web page. (And definitely not with the formatting they show.)
If you're unfamiliar enough with PHP that this issue wasn't immediately apparent to you, this is probably a sign that you should step back and get more familiar with PHP before you continue -- especially if you're going to be running code that could make you lose a lot of money if it isn't working properly.
You need to echo your $obj or at least var_dump() it to see the content on a webpage.

Calling file() on pastebin URL fails, but on local file or google.com it works

I'm working on a bit of PHP code that depends on a remote file which happens to be hosted on pastebin. The server I am working on has all the necessary functions enabled, as running it with FILE_URL set to http://google.com returns the expected results. I've also verified through php.ini for extra measure.
Everything should work, but it doesn't. Calling file() on a URL formed as such, http://pastebin.com/raw.php?i=<paste id here>, returns a 500 server error. Doing the same on the exact same file hosted locally or on google.com returns a reasonable result.
I have verified that the URL is set to the correct value and verified that the remote page is where I think that it is. I'm at a loss.
ini_set("allow_url_fopen", true);
// Prefer remote (up-to-date) file, fallback to local file
if( ini_get("allow_url_fopen") ){
$file = file( FILE_URL );
}
if(!isset( $file ) || !$file ) {
$file = file( LOCAL_FILE_PATH );
}
I wasn't able to test this, but you should use curl, try something like this:
<?php
$url = "http://pastebin.com/2ZdFcEKh";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_exec($ch);
Pastebin appear to use a protection system that will automatically block IP addresses that issue requests that are "bot-like".
In the case of your example, you will get a 500 server error since the file() command never completes (since their protection system never closes the connection) and there is no timeout facility in your call. The script is probably considered "bot-like" since file() does not pass through all the standard HTTP headers a typical browser would.
To solve this problem, I would recommend investigating cURL and perhaps look at setting a browser user agent as a starting point to grant access to your script. I should also mention that it would be in your interests to investigate whether or not this is considered a breach of the Pastebin user agreement. While I cannot see any reference to using scripts in their FAQ (as of 2012/12/29), they have installed protection against scripts for a reason.

PHP proxy server working half of the time

I know next to nothing about PHP. I'm trying to give away a set of financial calculators that rely on web service which is accessed via either a .NET proxy or PHP proxy. I've in stalled the PHP proxy on 3 different servers (windows and linux) and the setup always works for me. Yet, I have webmaster write and they can't get it to run.
I was hoping someone with debugging experice can give these a try:
http://www.pine-grove.com/online-calculators/pgs-html-calculators.htm
Here's more background to save you some time.
There an install PDF included. But basically unzip in a folder. Suggest "calculators". Locate js/calculator.js. At about row 11, edit this line to point to the proxy that is installed:
var strWebService = 'http://{www.your-server.com}/Calculators/proxies/calculators.php';
That's all that should be required. The HTTPRequest object's responseText field contains this error:
soap:ReceiverServer was unable to process request. ---> '\' is an unexpected token. The expected token is '"' or '''. Line 1, position 15.**
This seems to be working for most people, but for a handfull, it doesn't.
thanks in advance and I hope someone can shed some light on this problem.
A few things:
curl_setopt($ch, CURLOPT_HEADER, 1); is probably not what you want. This causes the response headers to be included as text at the head of $result. By echoing them back, they do not magically become response headers of the request to calculator.php; they are part of the body of the response.
A four second timeout is probably too small. If it works for you, but not the webmaster, then I'm guessing that the CURL request performed by calculator.php timed out for the webmaster and a warning stating this fact was sent back.
These look wrong:
$header[] = "Content-Type: text/xml; charset=utf-8";
$header[] = 'http://com.pine-grove/' + $wsMethod;
Get rid of the PHP close tag ?> at the end. It's generally not needed and you risk sending back extra whitespace, as in this case, where calculator.php is inadvertently appending CR+LF to the bodies of all proxied responses.

file_get_contents() GET request not showing up on my webserver log

I've got a simple php script to ping some of my domains using file_get_contents(), however I have checked my logs and they are not recording any get requests.
I have
$result = file_get_contents($url);
echo $url. ' pinged ok\n';
where $url for each of the domains is just a simple string of the form http://mydomain.com/, echo verifies this. Manual requests made by myself are showing.
Why would the get requests not be showing in my logs?
Actually I've got it to register the hit when I send $result to the browser. I guess this means the webserver only records browser requests? Is there any way to mimic such in php?
ok tried curl php:
// create curl resource
$ch = curl_init();
// set url
curl_setopt($ch, CURLOPT_URL, "getcorporate.co.nr");
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// $output contains the output string
$output = curl_exec($ch);
// close curl resource to free up system resources
curl_close($ch);
same effect though - no hit registered in logs. So far it only registers when I feed the http response back from my script to the browser. Obviously this will only work for a single request and not a bunch as is the purpose of my script.
If something else is going wrong, what debugging output can I look at?
Edit: D'oh! See comments below accepted answer for explanation of my erroneous thinking.
If the request is actually being made, it would be in the logs.
Your example code could be failing silently.
What happens if you do:
<?PHP
if ($result = file_get_contents($url)){
echo "Success";
}else{
echo "Epic Fail!";
}
If that's failing, you'll want to turn on some error reporting or logging and try to figure out why.
Note: if you're in safe mode, or otherwise have fopen url wrappers disabled, file_get_contents() will not grab a remote page. This is the most likely reason things would be failing (assuming there's not a typo in the contents of $url).
Use curl instead?
That's odd. Maybe there is some caching afoot? Have you tried changing the URL dynamically ($url = $url."?timestamp=".time() for example)?

Categories