I need to post some XML to an API on our network via php. Usually I would use Curl to do this which I know works well but the server my scripts run from doesnt have curl and without going into all the detail it cannot be installed as it breaks some other important production app on the server.
After some thought I was hoping to post my XML via jquery to the API. I was wondering if anyone knoew if it was possible to access jquery from within a PHP class? I know it sounds crazy and im sure someone will tell me why I shouldnt do this but to me it sounds like a reasonable solution.
Othwerwise I might have to go though the class and try and pull the xml back to the controller and page that uses the class to send the XML.
P.S we have tried command line curl to solve our issue which does work however we get an issue with URL length on some requests as the XML can be very large and too long to pass via url.
Any thoughts?
Use sockets instead.
Copied from the link @Val posted:
<?php
$fp = fsockopen("www.example.com", 80, $errno, $errstr, 30);
if (!$fp)
{
echo 'Could not open connection.';
}
else
{
$xmlpacket ='<?XML version="1.0"?>
<Your_XML>
</Your_XML>';
$contentlength = strlen($xmlpacket);
$out = "POST /script_name.php HTTP/1.0
";
$out .= "Host: www.example.com
";
$out .= "Connection: Keep-Alive
";
$out .= "Content-type: application/x-www-form-urlencoded
";
$out .= "Content-length: $contentlength
";
$out .= "XML=$xmlpacket";
fwrite($fp, $out);
while (!feof($fp))
{
$theOutput .= fgets($fp, 128);
}
fclose($fp);
// $theOutput is the response returned from the remote script
}
?>
You can't. jQuery runs in the browser at the client not on the server. You can send Javascript to the client and do your stuff there. But with file_put_contents you can also send data to another side, if it accepts it per GET.