Hola estoy probando el uso de FSOCKOPEN como muestro a continuacion me dice 200 OK, la linea como tal esta correcta pero el script me dice que la url esta malformanda.
Esta es la linea que tengo que pasar:
name=usuario@gmail.com&pass=password&message=mensaje+de+Probando&number=53.52653526&ownnum=cheito
Respuesta:
name=usuario@gmail.com&pass=password&message=mensaje+de+Probando&number=53.52653526&ownnum=cheitoHTTP/1.1 200 OK Date: Thu, 15 Sep 2011 16:07:44 GMT Server: Apache X-Powered-By: PHP/5.2.17 Set-Cookie: countmdw=Si; expires=Thu, 15-Sep-2011 16:17:44 GMT Connection: close Transfer-Encoding: chunked Content-Type: text/html 20 result=error^reason=malformedsms 0
Que puede estar pasando, me llama la atencion y es el 20 que esta delante del result=error^reason=malformedsms 0
Le pongo el codigo que estoy usando por si ven algo raro..
$fp = fsockopen("www.hostxxx.com", 80, $errno, $errstr, 30);
fputs($fp, "POST /api.php HTTP/1.1\r\n");
fputs($fp, "Host: www.hostxxx.com\r\n");
fputs($fp, "Content-type: application/x-www-form- urlencoded\r\n");
fputs($fp, "Content-length: " . strlen($vars) . "\r\n");
fputs($fp, "User-agent: Mozilla/4.0 (compatible: MSIE 7.0; Windows NT 6.0)\r\n");
fputs($fp, "Connection: close\r\n\r\n");
fputs($fp, $vars);
while (!feof($fp)) {
$buf .= fgets($fp,128);
}
fclose($fp);
Espero por sugerencias..
mmm se me ocurre utilizar urlencode para pasar el string, no lo sé, igual y así te funque. Prueba enviando cadenas con caracteres simples (1234567890, abce..etc...).
Suerte.
Tal vez es porque tienes que enviar la cabecera toda de una vez.
Primero guardas la cabecera en una variable y luego haces el fput().
$header = "";
$fp = fsockopen("www.hostxxx.com", 80, $errno, $errstr, 30;
$header .= "POST /api.php HTTP/1.1\r\n";
$header .= "Host: www.hostxxx.com\r\n";
$header .= "Content-type: application/x-www-form- urlencoded\r\n";
$header .=, "Content-length: " . strlen($vars) . "\r\n";
$header .="User-agent: Mozilla/4.0 (compatible: MSIE 7.0; Windows NT 6.0)\r\n";
$header .= "Connection: close\r\n\r\n";
fputs($fp, $headers);
while (!feof($fp)) {
$buf .= fgets($fp,128);
}
fclose($fp);
Si sigue sin funcionar, intenta hacerlo con cURL, hay varios ejemplos en el foro.
Mira este ejemplo desde php.net, talves te sirva.
Es una petición POST sin usar fsock ni curl:
http://php.net/manual/es/function.file-get-contents.php
<?php
/**
make an http POST request and return the response content and headers
@param string $url url of the requested script
@param array $data hash array of request variables
@return returns a hash array with response content and headers in the following form:
array ('content'=>'<html></html>'
, 'headers'=>array ('HTTP/1.1 200 OK', 'Connection: close', ...)
)
*/
function http_post ($url, $data){
$data_url = http_build_query($data);
$data_len = strlen($data_url);
return array(
'content' => file_get_contents($url, false, stream_context_create(array(
'http'=> array(
'method' => 'POST',
'header' => "Connection: close\r\nContent-Length: $data_len\r\n",
'content' => $data_url
)
))),
'headers' => $http_response_header
);
}
?>
Yo para casi todo utilizo el file_get_contents(), además se adapta solo a multiples protocolos sin tener que escribir todo a mano, se adapta a https, ftps, http, ftp, hasta obtener archivos locales, etc.
Es una verdadera navaja suiza de las peticiones simples.
Se usa masomenos así:
<?php print_r(http_post('http://www.host.com/archivo.php', array('var1' => 'val1'))); ?>
Creo que la division entre la url y los parámetros enviados, debe ser separara por un ? y no por un &.