PHP cURL to many urls - php

I have php a code and can't understand why the php script creates only 4 files, but without the data. If I use curl only for one url without foreach loop all will be ok. Any thoughts?
$years = array('2012', '2013', '2014', '2015');
foreach($years as $year)
{
$url = "http://lpo.dt.navy.mil/data/DM/Environmental_Data_Deep_Moor_{$year}.txt";
$fp = fopen(base_path() . "/database/rawdata/{$year}.txt", 'w');
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_FILE => $fp,
CURLOPT_VERBOSE => 1,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_FAILONERROR => 1
));
curl_exec($curl);
if (!curl_errno($curl))
{
$info = curl_getinfo($curl);
Log::info("File created {$year}.txt");
Log::notice("Evaluation of this script was {$info['total_time']} seconds!");
}
else
{
Log::error("cURL ERROR " . curl_error($curl));
}
curl_close($curl);
fclose($fp);
}

See joeterranova's comment on the PHP site:
It appears that setting CURLOPT_FILE before setting
CURLOPT_RETURNTRANSFER doesn't work, presumably because
CURLOPT_FILE depends on CURLOPT_RETURNTRANSFER being set
.
Therefore you should change your curl_setopt_array to the following:
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_FILE => $fp,
CURLOPT_VERBOSE => 1,
CURLOPT_FAILONERROR => 1
));

Related

Submitting with PHP and Curl

I've been trying all morning to get an XML string uploaded via an API so that it submits my order but no matter what I try it simply isn't working for me.
My URL:
$url = "http://example.com/SubmitOrder?apiKey=ABC123&clientID=MYId&orderXml=".$xml;
$xml is my xml details already pre-formatted.
I then put this into my curl section:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $url
));
$resp = curl_exec($curl);
$xml1=simplexml_load_string($resp) or die("Error: Cannot create object");
print_r($xml1);
echo "Submitted";
The response I get is "Error: Cannot create object" and I can see that my details have not been submitted.
Where am iI going wrong ??
Many thanks.
You can check your response data and create data in this code example.
<?php
$url = 'https://www.w3schools.com/xml/note.xml';
$resp = file_get_contents($url);
if (resp) {
$string = simplexml_load_string($resp);
var_dump($movies);
}
but if you want get data in method curl try this.
<?php
$html_brand = 'https://www.w3schools.com/xml/note.xml';
$ch = curl_init();
$options = array(
CURLOPT_URL => $html_brand,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_ENCODING => "",
CURLOPT_AUTOREFERER => true,
CURLOPT_CONNECTTIMEOUT => 120,
CURLOPT_TIMEOUT => 120,
CURLOPT_MAXREDIRS => 10,
);
curl_setopt_array( $ch, $options );
$response = curl_exec($ch);
switch ($http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE)) {
case 200: # OK
break;
default:
echo 'Status code HTTP: ', $http_code, "\n";
}
var_dump($response);
curl_close($ch);
die();
Check your data. And check your response status code.

Issue uploading to file sharing website PHP

Currently I have the following code.
error_reporting(E_ALL);
ini_set('display_errors', 1);
include('simple_html_dom.php');
$df = upload('username','password','song.mp3','song.mp3');
$df = json_decode($df, true);
function upload($login,$password,$filename,$file) {
$ch = curl_init();
$postData = array(
'op' => 'login',
'login' => $login,
'password' => $password,
'returnto' => '/'
);
curl_setopt_array($ch, array(
CURLOPT_URL => 'http://dopefile.pk/login.html',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $postData,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_COOKIESESSION => true,
CURLOPT_COOKIEJAR => 'cookie.txt'
));
$output = curl_exec($ch);
curl_setopt($ch, CURLOPT_URL, "http://dopefile.pk/?op=upload");
$output = curl_exec($ch);
$html = str_get_html($output);
$ret1 = $html->find('form[id=uploadfile]');
$ret = $html->find('form[id=uploadfile] input');
foreach($ret AS $r) {
$newPost[$r->name]=$r->value;
}
$newPost['file_0'] = '#'.$file.';filename='.$filename;
curl_setopt_array($ch, array(
CURLOPT_URL => 'http://dopefile.pk/cgi-bin/upload.cgi?upload_type=file',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $newPost,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_COOKIESESSION => true,
CURLOPT_COOKIEJAR => 'cookie.txt'
));
return $output = curl_exec($ch);
}
Which is trying to upload to the file sharing website the file song.mp3 which is within my directory, and then return the file sharing websites link (if you download the simple_html_dom.php files and try and run the code above, you can see that it returns no errors or returns no file sharing link + does not upload to my account on the website...
The cookies file updates with the cookies that show I successfully logged in, how ever I can't get the actual file link returned can anyone help me please?

Error with file_get_contents() [duplicate]

How can I read a response from Stackoverflow API in PHP? The response is GZIP-ed. I found e.g. the following suggestion:
$url = "http://api.stackoverflow.com/1.1/questions/" . $question_id;
$data = file_get_contents($url);
$data = http_inflate($data);
but the function http_inflate() is not available on the installation that I am using.
Are there some other easy ways to accomplish it?
A cool way
http://www.php.net/manual/en/wrappers.compression.php
Notice the use of a stream wrapper, compress.zlib
$url = "compress.zlib://http://api.stackoverflow.com/1.1/questions/" . $question_id;
echo $data = file_get_contents($url, false, stream_context_create(array('http'=>array('header'=>"Accept-Encoding: gzip\r\n"))));
or using curl
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => $url
, CURLOPT_HEADER => 0
, CURLOPT_RETURNTRANSFER => 1
, CURLOPT_ENCODING => 'gzip'
));
echo curl_exec($ch);
edited--
other methods removed because they don't send an Accept-Encoding http header.
Use this to get gzip encoding json response its working like a charm
function get_gzip_json($url) {
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => $url,
CURLOPT_HEADER => 0,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_ENCODING => 'gzip',
));
$json = curl_exec($ch);
$json= mb_convert_encoding($json, "UTF-8", "UTF-8");
return $json;
}

PHP Curl does not send POST request to server

I have this code:
<?php
$data = array('name' => 'Ross', 'php_master' => true);
$url = 'http://dsaasd.adsds.nl:8081/cops.nsf/orders?openagent';
$html_brand = "www.google.com";
$ch = curl_init();
$options = array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_ENCODING => "",
CURLOPT_AUTOREFERER => true,
CURLOPT_CONNECTTIMEOUT => 120,
CURLOPT_TIMEOUT => 120,
CURLOPT_MAXREDIRS => 10,
CURLOPT_VERBOSE => true,
);
curl_setopt_array( $ch, $options );
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ( $httpCode != 200 ){
echo "Return code is {$httpCode} \n"
.curl_error($ch);
} else {
echo "<pre>".htmlspecialchars($response)."</pre>";
}
curl_close($ch);
?>
The problem is, if i send curl post to:
$url = 'http://dsaasd.adsds.nl:8081/cops.nsf/orders?openagent';
Then it does NOT work.
But if i send curl post to the same service but on a different server then it works, this is the other server
$url = 'http://dsaasd.adsds.nl:80/cops.nsf/orders?openagent';
I also post data by normal form post to:
$url = 'http://dsaasd.adsds.nl:8081/cops.nsf/orders?openagent';
And then it works and i receive data on the server.
But with this curl post i keep getting:
Return code is 0 Failed to connect to 11.43.45.123: Network is unreachable
Anyone have any idea?
I think you need to add CURLOPT_POST to the $options array. Docs here.

header parameters post request php ( curl)

I' m trying to build a php script that post other webservice I'm using culr as you can see, the issue is in the API webserver docs ask me to send some header parameters, literally
"For each http call you will need to add the following header parameters:
APIuserID,
APIpassword and
ResponseType"
so my question is how can I add custom parameters to my header request?. thanks
<?php
function curl_post($url, array $post = NULL, array $options = array())
{
$defaults = array(
CURLOPT_POST => 1,
CURLOPT_HEADER => 0,
CURLOPT_URL => $url,
CURLOPT_FRESH_CONNECT => 1,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_FORBID_REUSE => 1,
CURLOPT_TIMEOUT => 4,
CURLOPT_SSL_VERIFYHOST=>0,
CURLOPT_POSTFIELDS => http_build_query($post)
);
$ch = curl_init();
curl_setopt_array($ch, ($options + $defaults));
if( ! $result = curl_exec($ch))
{
trigger_error(curl_error($ch));
}
curl_close($ch);
return $result;
}
$result=curl_post('https://www.serverPath/serverAPI',array('a'=>'a','b'=>'b'));
echo $result;
?>
Have you tried by just adding those parameters to the httpheader?
curl_setopt($tuCurl, CURLOPT_HTTPHEADER, array(
"Content-Type: text/xml",
"APIuserID: $id",
"APIpassword: $password",
"Content-length: ".strlen($data)
));
Additionally you can use: http://php.net/manual/en/soapheader.soapheader.php

Categories