0

Would like some help with my php curl connection - in my firebug console I keep getting this error

Notice: Undefined index: host in C:\xampp\htdocs\labs\test2\get.php on line 6
Error:3 malformed

AJAX code:

    var hostName = $("input#host").val();
dataString = hostName;

$.ajax({
    type: "GET",
    url: "get.php",
    data: dataString,
    dataType: "json",

PHP CURL code:

<?php
if($fp = tmpfile())
{
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $_GET['host']);
    curl_setopt($ch, CURLOPT_STDERR, $fp);
    curl_setopt($ch, CURLOPT_CERTINFO, 1);
    curl_setopt($ch, CURLOPT_VERBOSE, 1);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_NOBODY, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,  2);
    $result = curl_exec($ch);
    curl_errno($ch)==0 or die("Error:".curl_errno($ch)." ".curl_error($ch));
    fseek($fp, 0);//rewind
    $str='';
    while(strlen($str.=fread($fp,8192))==8192);
    echo $str;
    fclose($fp);
}
?>

-- RESPONSE ---

HTTP/1.1 302 Found Cache-Control: private Content-Type: text/html; charset=UTF-8 Location: http://www.google.com.au/?gfe_rd=cr&ei=VbwrVa_RFsPu8wfN54HYBQ Content-Length: 262 Date: Mon, 13 Apr 2015 12:53:41 GMT Server: GFE/2.0 Alternate-Protocol: 80:quic,p=0.5 * Rebuilt URL to: www.google.com/ * Hostname was NOT found in DNS cache * Trying 216.58.220.100... * Connected to www.google.com (216.58.220.100) port 80 (#0) > HEAD / HTTP/1.1 Host: www.google.com Accept: / < HTTP/1.1 302 Found < Cache-Control: private < Content-Type: text/html; charset=UTF-8 < Location: http://www.google.com.au/?gfe_rd=cr&ei=VbwrVa_RFsPu8wfN54HYBQ < Content-Length: 262 < Date: Mon, 13 Apr 2015 12:53:41 GMT < Server: GFE/2.0 < Alternate-Protocol: 80:quic,p=0.5 < * Connection #0 to host www.google.com left intact

3
  • Shouldn't it be data: {host: hostName}, ? Commented Apr 13, 2015 at 12:19
  • What are you using http_build_query for? You're effectively setting $url to "http://host=www.google.com", which as curl says, is not a valid hostname. Commented Apr 13, 2015 at 12:39
  • yeah it should only pass the value which would be the URL. If i have data {hostName], i still get errors in the console <b>Notice</b>: Undefined index: host in <b>C:\xampp\htdocs\labs\test2\get.php</b> on line <b>6</b><br />Error:3 <url> malformed Commented Apr 13, 2015 at 12:43

2 Answers 2

3

http_build_query does not create a valid url but only formats the parameters array for your GET request. You should do something like :

$url = "https://".$_REQUEST['host']."?"."{$query_string}";

In your example the resulting url will be

https://www.google.com?host=www.google.com

Note the "https"

Sign up to request clarification or add additional context in comments.

7 Comments

sure thx for the suggestion.. i can a response but i think something is wrong still.. every site i test i get this response in console Error:60 SSL certificate problem: unable to get local issuer certificate
expected output can be seen here link
Look into your apache (or whatever) error log for the curl ssl output info. As for the SSL certificate error, I am not sure but there is an answer there about that : stackoverflow.com/questions/21187946/… You can either modify this line : curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); or install the correct certificates (depends on your platform).
i found the file i needed to reference and am one step closer curl_setopt($ch, CURLOPT_CAINFO, 'cacert.pem'); now getting this in the console log Error:77 error setting certificate verify locations: ..will keep looking
all done.. just had to set the path to the cacert.pem file in the curl_setopt($ch, CURLOPT_CAINFO, 'c:\path\to\file\cacert.pem'); and now i get the ssl cert extract in the console :)
|
3

Your data string is wrong

dataString = hostName;

That will only contain the data

dataString = {"host":hostName};

That should pass the parameter host in your GET string

5 Comments

tried it and now get this error in firebug Error:6 Could not resolve host: host=www.google.com
Why are you setting the URL to the value of the query string? Try curl_setopt($ch, CURLOPT_URL, $_GET['host']);. I would not leave that code in production, however, as anyone could use your script to spam another site with requests.
okay, i updated my post with the php code with the changes you suggested.. i also included the response im getting. definetly an improvement but still not getting the desired outcome as seen here link
You're still not connecting over SSL. Connected to www.google.com (216.58.220.100) port 80 (#0) Port 80 is http://. You want 443, which is https://. You will either have to pass that in your GET or change your code to only accept https URLs
yup. if i use https:// it connects to 443 but gives this error Error:60 SSL certificate problem: unable to get local issuer certificate i seem to recall curl needs to be told where to reference the cerificate authorities file to trust the connection.. i will do some googling for it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.