0

i need to send get command and take it's results. Sorry about my bad english.
i need to get result from a file whish sended url parameters for example:

<?php

$adata["command1"] = "testcommand1";
$adata["command2"] = "testcommand2";
$getresult = sendGetCommand("https://website.com/api.html", $arrayofdata);

echo "["; // for json data;

$arrayresult = explode("\n",$getresult);
foreach ($getresult in $line) {
  $arrayline = explode("\n",$line);
   echo "{ ";
  foreach ($arrayline in $cmdid => $cmd) {
    echo "'".$cmdid."' : '".$cmd."',";
  }
  echo "{";
}
?>

somethink like this..

url is like:
"https://website.com/api.html?command1=testcommand1&command2=testcommand2"

url result is like:


command1,testcommand1,,yes
command2,testcommand2,,error,error text here


i'll explode the data line by line and then get the data from JavaScript this is a domain search api.

another question: explode("\n",$string) can be used for read it line by line? (windows os)

2 Answers 2

4

are you talking about file_get_contents? you can create the url with something like:

$url = "https://website.com/api.html?command1=".$adata["command1"]."&command2=".$adata["command2"];
$getresult = file_get_contents($url);

good luck;

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

Comments

2

for reading the result, you should take a look at str_getcsv and/or fgetcsv instead of doing this by hand using explode.

EDIT: for sending a get-request, you should take a look at fsockopen and its examples. you could use a function like this (just change POST to GET and the content-type like you need it):

function _get($type,$host,$port='80',$path='/',$data='') {
    $_err = 'lib sockets::'.__FUNCTION__.'(): ';
    switch($type) { case 'http': $type = ''; case 'ssl': continue; default: die($_err.'bad $type'); } if(!ctype_digit($port)) die($_err.'bad port');
    if(!empty($data)) foreach($data AS $k => $v) $str .= urlencode($k).'='.urlencode($v).'&'; $str = substr($str,0,-1);

    $fp = fsockopen($host,$port,$errno,$errstr,$timeout=30);
    if(!$fp) die($_err.$errstr.$errno); else {
        fputs($fp, "POST $path HTTP/1.1\r\n");
        fputs($fp, "Host: $host\r\n");
        fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n");
        fputs($fp, "Content-length: ".strlen($str)."\r\n");
        fputs($fp, "Connection: close\r\n\r\n");
        fputs($fp, $str."\r\n\r\n");

        while(!feof($fp)) $d .= fgets($fp,4096);
        fclose($fp);
    } return $d;
} 

3 Comments

fputs($fp, "POST $path HTTP/1.1\r\n"); is this useable for POST request?
@dreamlore: i can't understand such a comment... maybe you can try to write complete sentences. if you're confused by "POST": read my post again: "(just change POST to GET and the content-type like you need it)"
sorry i mean GET request. ok i got my answer. thanks for edit. i'll try 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.