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

user3436467
  • 1,763
  • 1
  • 22
  • 35
  • Shouldn't it be `data: {host: hostName},` ? – David Kmenta Apr 13 '15 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. – mario Apr 13 '15 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 `Notice: Undefined index: host in C:\xampp\htdocs\labs\test2\get.php on line 6
    Error:3 malformed`
    – user3436467 Apr 13 '15 at 12:43

2 Answers2

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

Machavity
  • 30,841
  • 27
  • 92
  • 100
  • tried it and now get this error in firebug `Error:6 Could not resolve host: host=www.google.com` – user3436467 Apr 13 '15 at 12:27
  • 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. – Machavity Apr 13 '15 at 12:50
  • 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](http://www.zedwood.com/article/php-fetch-ssl-certificate-using-curl) – user3436467 Apr 13 '15 at 12:58
  • 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 – Machavity Apr 13 '15 at 13:18
  • 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 – user3436467 Apr 13 '15 at 13:24
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"

  • 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** – user3436467 Apr 13 '15 at 13:08
  • expected output can be seen here [link](http://www.zedwood.com/article/php-fetch-ssl-certificate-using-curl) – user3436467 Apr 13 '15 at 13:11
  • 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 : http://stackoverflow.com/questions/21187946/60-ssl-certificate-self-signed-certificate-in-certificate-chainbool You can either modify this line : curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); or install the correct certificates (depends on your platform). – Denis Rafin Apr 13 '15 at 13:43
  • 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 – user3436467 Apr 13 '15 at 13:56
  • 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 :) – user3436467 Apr 13 '15 at 14:04
  • cacert.pem file can be downloaded from [link](http://curl.haxx.se/docs/caextract.html) – user3436467 Apr 13 '15 at 14:06
  • Great ! Happy you found the solution. – Denis Rafin Apr 13 '15 at 14:07