I am using curl to fetch result from Punchtab when I execute this code I am getting curl exception 3 No URL set! can any one help me with it.
$tocken2 = "https://api.punchtab.com/v1/user?access_token=".$rest->authResponse->accessToken;
$ch1 = curl_init();
$ca = 'c:/path/to/ca-bundle.crt';
curl_setopt($ch, CURLOPT_URL,$tocken2);
curl_setopt ($ch1, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt ($ch1, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch1, CURLOPT_POST, 1);
curl_setopt($ch1, CURLOPT_REFERER, "http://locatefirms.in");
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch1);
$errorMsg = curl_error($ch1);
$errorNumber = curl_errno($ch1);
echo $errorMsg;
echo $errorNumber;
echo $server_output;exit;
asked Nov 29, 2012 at 6:50
1
Your curl handle is $ch1
, but you’re setting the URL option to $ch
.
$ch1 = curl_init(); // initializing $ch1 here
curl_setopt($ch, CURLOPT_URL,$tocken2); // setting option to $ch
Quite obviously, to fix this, you need to change the statement to
curl_setopt($ch1, CURLOPT_URL,$tocken2);
answered Nov 29, 2012 at 7:00
AyushAyush
41.3k49 gold badges161 silver badges237 bronze badges
0
This PHP allows the user to submit a form with a URL in binary or plain text. The URL is then converted to plain text and I’m using cURL to load the response then translate it back to binary. In case you’re wondering, it’s from a binary translator which can translate text -> binary and binary -> text, but also accepts URLs.
The problem: As you can see binary URLs are translated to text then passed to cURL. The plaintext value is stored in $newtext
. I can confirm that binaryToText()
does work as advertised through some debugging I did. Plain text URLs (see the else
portion of the if statement) are successfully set, but converted binary ones are not.
Examples
e.g. $text = "http://google.co.uk";
(isBinary($text) == false
)
curl_setopt($ch, CURLOPT_URL, $text);
<- This works
e.g. $text = "01101000011101000111010001110000001110100010111100101111011001110110111101101111011001110110110001100101001011100110001101101111001011100111010101101011"
(same google url)
(isBinary($text) == true
)
$newtext = binaryToText($text);
curl_setopt($ch, CURLOPT_URL, $newtext);
echo curl_error($ch);
-> outputs «No URL set!»
Somewhere in there is the problem… I can’t see it though.
Code listing
$text = $_POST["text"];
if(startsWith($text, "http://") || startsWith($text, "https://") || startsWith($text, textToBinary("http://")) || startsWith($text, textToBinary("https://"))) {
// URL - accepts binary (prefered), or plain text url; returns binary.
$ch = curl_init();
if(isBinary($text)) {
$newtext = binaryToText($text);
curl_setopt($ch, CURLOPT_URL, $newtext);
} else {
curl_setopt($ch, CURLOPT_URL, $text);
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, "MY USERAGENT");
if(isset($_GET["r"])) curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$result=curl_exec($ch);
echo curl_error($ch);
curl_close($ch);
echo textToBinary($result);
} else if...
Functions
function isBinary($input) {
return !preg_match('/[^(0|1)]/', $input); // contains nothing but 0 and 1
}
function binaryToText($input) {
$return = '';
$chars = explode("n", chunk_split(str_replace("n", '', $input), 8));
$_I = count($chars);
for($i = 0; $i < $_I; $return .= chr(bindec($chars[$i])), $i++);
return $return;
}
EDIT:
Output of $newtext
is contained in this output:
text: 01101000011101000111010001110000001110100010111100101111011001110110111101101111011001110110110001100101001011100110001101101111001011100111010101101011
newtext: http://google.co.uk
No URL set!00000000
From this:
if(isBinary($text)) {
echo "text: ".$text."n";
$newtext = binaryToText($text);
echo "newtext: ".$newtext."n";
curl_setopt($ch, CURLOPT_URL, $newtext);
} else {
curl_setopt($ch, CURLOPT_URL, $text);
}
You have substituted -o
(lower case) in place of -O
(upper case), causing the URL to be interpreted as the name of an output file. From man curl
:
-o, --output <file>
Write output to <file> instead of stdout.
versus
-O, --remote-name
Write output to a local file named like the remote file we get.
answered Feb 26, 2021 at 13:19
steeldriversteeldriver
127k21 gold badges226 silver badges311 bronze badges
The -o (small alphabet o) is used to output to a file.
When you use this option curl uses the next argument as the name of file and expects another argument for the URL.
The tutorial that you have mentioned uses the -O (capital alphabet O) option.
You can learn more about these by running
curl --help
answered Feb 26, 2021 at 13:19